Changeset 4738

Show
Ignore:
Timestamp:
05/10/07 16:22:34 (2 years ago)
Author:
morris
Message:

Some temporary refactions

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • wired/trunk/wired/accounts.c

    r4508 r4738  
    604604                return; 
    605605         
     606        wd_client_wrlock_client(client); 
     607         
    606608        wi_retain(account); 
    607609        wi_release(client->account); 
     
    609611        client->account = account; 
    610612 
    611         wi_lock_lock(client->flag_lock); 
    612613        admin = client->admin; 
    613614        client->admin = (client->account->kick_users || client->account->ban_users); 
    614         wi_lock_unlock(client->flag_lock); 
     615 
     616        wd_client_unlock_client(client); 
    615617         
    616618        if(client->admin != admin) 
  • wired/trunk/wired/chats.c

    r4576 r4738  
    132132wd_chat_t * wd_chat_init(wd_chat_t *chat) { 
    133133        chat->clients = wi_array_init(wi_array_alloc()); 
     134        chat->lock = wi_rwlock_init(wi_rwlock_alloc()); 
    134135         
    135136        return chat; 
     
    200201#pragma mark - 
    201202 
     203void wd_chat_wrlock(wd_chat_t *chat) { 
     204        wi_rwlock_wrlock(chat->lock); 
     205} 
     206 
     207 
     208 
     209void wd_chat_rdlock(wd_chat_t *chat) { 
     210        wi_rwlock_rdlock(chat->lock); 
     211} 
     212 
     213 
     214 
     215void wd_chat_unlock(wd_chat_t *chat) { 
     216        wi_rwlock_unlock(chat->lock); 
     217} 
     218 
     219 
     220 
     221#pragma mark - 
     222 
    202223wi_boolean_t wd_chat_contains_client(wd_chat_t *chat, wd_client_t *client) { 
    203         wi_enumerator_t *enumerator; 
    204         wd_client_t             *each; 
    205224        wi_boolean_t    contains = false; 
    206225 
    207226        if(chat) { 
    208                 wi_array_rdlock(chat->clients); 
    209                  
    210                 enumerator = wi_array_data_enumerator(chat->clients); 
    211                  
    212                 while((each = wi_enumerator_next_data(enumerator))) { 
    213                         if(each == client) { 
    214                                 contains = true; 
    215  
    216                                 break; 
    217                         } 
    218                 } 
    219                          
    220                 wi_array_unlock(chat->clients); 
     227                wd_chat_rdlock(chat); 
     228                contains = wi_array_contains_data(chat->clients, client); 
     229                wd_chat_unlock(chat); 
    221230        } 
    222231 
     
    227236 
    228237void wd_chat_add_client(wd_chat_t *chat, wd_client_t *client) { 
     238        wd_client_rdlock_client(client); 
    229239        wd_broadcast(chat, 302, WI_STR("%u%c%u%c%u%c%u%c%u%c%#@%c%#@%c%#@%c%#@%c%#@%c%#@"), 
    230240                                 chat->cid,                     WD_FIELD_SEPARATOR, 
     
    239249                                 client->status,        WD_FIELD_SEPARATOR, 
    240250                                 client->image); 
    241          
    242         wi_array_wrlock(chat->clients); 
     251        wd_client_unlock_client(client); 
     252         
     253        wd_chat_wrlock(chat); 
    243254        wi_array_add_data(chat->clients, client); 
    244         wi_array_unlock(chat->clients); 
     255        wd_chat_unlock(chat); 
    245256 
    246257        if(chat->topic.topic) 
     
    251262 
    252263void wd_chat_remove_client(wd_chat_t *chat, wd_client_t *client) { 
    253         wi_array_wrlock(chat->clients); 
     264        wd_chat_wrlock(chat); 
    254265        wi_array_remove_data(chat->clients, client); 
    255         wi_array_unlock(chat->clients); 
     266        wd_chat_unlock(chat); 
    256267 
    257268        if(chat->cid != WD_PUBLIC_CID && wi_array_count(chat->clients) == 0) { 
     
    268279        wd_client_t             *client; 
    269280         
    270         wi_array_rdlock(chat->clients); 
     281        wd_chat_rdlock(chat); 
    271282         
    272283        enumerator = wi_array_data_enumerator(chat->clients); 
    273284         
    274285        while((client = wi_enumerator_next_data(enumerator))) { 
     286                wd_client_rdlock_client(client); 
     287                 
    275288                if(client->state == WD_CLIENT_STATE_LOGGED_IN) { 
    276289                        wd_reply(310, WI_STR("%u%c%u%c%u%c%u%c%u%c%#@%c%#@%c%#@%c%#@%c%#@%c%#@"), 
     
    287300                                         client->image); 
    288301                } 
     302 
     303                wd_client_unlock_client(client); 
    289304        } 
    290305         
    291         wi_array_unlock(chat->clients); 
     306        wd_chat_unlock(chat); 
    292307 
    293308        wd_reply(311, WI_STR("%u"), chat->cid); 
     
    301316        wd_client_t             *client = wd_client(); 
    302317         
     318        wd_chat_wrlock(chat); 
     319         
    303320        wi_release(chat->topic.topic); 
    304321        chat->topic.topic = wi_copy(topic); 
     
    315332        wi_release(chat->topic.ip); 
    316333        chat->topic.ip = wi_copy(client->ip); 
     334 
     335        wd_chat_unlock(chat); 
    317336} 
    318337 
     
    322341        wi_string_t             *string; 
    323342         
     343        wd_chat_rdlock(chat); 
     344 
    324345        string = wi_date_iso8601_string(chat->topic.date); 
    325346         
     
    331352                         string,                                WD_FIELD_SEPARATOR, 
    332353                         chat->topic.topic); 
     354 
     355        wd_chat_unlock(chat); 
    333356} 
    334357 
     
    338361        wi_string_t             *string; 
    339362         
     363        wd_chat_rdlock(chat); 
     364 
    340365        string = wi_date_iso8601_string(chat->topic.date); 
    341366         
     
    347372                                 string,                                WD_FIELD_SEPARATOR, 
    348373                                 chat->topic.topic); 
     374 
     375        wd_chat_unlock(chat); 
    349376} 
    350377 
  • wired/trunk/wired/chats.h

    r4508 r4738  
    5757        wd_topic_t                                                      topic; 
    5858        wi_array_t                                                      *clients; 
     59         
     60        wi_rwlock_t                                                     *lock; 
    5961}; 
    6062typedef struct _wd_chat                                 wd_chat_t; 
     
    7476wd_chat_t *                                                             wd_chat_init_private_chat(wd_chat_t *); 
    7577 
     78void                                                                    wd_chat_wrlock(wd_chat_t *); 
     79void                                                                    wd_chat_rdlock(wd_chat_t *); 
     80void                                                                    wd_chat_unlock(wd_chat_t *); 
     81 
    7682wi_boolean_t                                                    wd_chat_contains_client(wd_chat_t *, wd_client_t *); 
    7783void                                                                    wd_chat_add_client(wd_chat_t *, wd_client_t *); 
  • wired/trunk/wired/clients.c

    r4576 r4738  
    5656static wi_timer_t                                               *wd_clients_timer; 
    5757 
     58static wd_uid_t                                                 wd_clients_current_uid; 
     59static wi_lock_t                                                *wd_clients_uid_lock; 
     60 
    5861wi_hash_t                                                               *wd_clients; 
    5962 
     
    7376 
    7477        wd_clients = wi_hash_init(wi_hash_alloc()); 
     78         
     79        wd_clients_uid_lock = wi_lock_init(wi_lock_alloc()), 
    7580 
    7681        wd_clients_timer = wi_timer_init_with_function(wi_timer_alloc(), 
     
    101106                 
    102107                while((client = wi_enumerator_next_data(enumerator))) { 
    103                         if(client->idle || client->state != WD_CLIENT_STATE_LOGGED_IN) 
    104                                 continue; 
    105  
    106                         if(client->idle_time + wd_settings.idletime < interval) { 
     108                        wd_client_wrlock_client(client); 
     109                         
     110                        if(client->state == WD_CLIENT_STATE_LOGGED_IN && 
     111                           !client->idle && 
     112                           client->idle_time + wd_settings.idletime < interval) { 
    107113                                client->idle = true; 
    108114 
    109115                                wd_client_broadcast_status(client); 
    110116                        } 
     117 
     118                        wd_client_unlock_client(client); 
    111119                } 
    112120        } 
     
    173181        client->state                   = WD_CLIENT_STATE_CONNECTED; 
    174182        client->login_time              = wi_time_interval(); 
    175         client->idle_time               = client->idle_time; 
     183        client->idle_time               = client->login_time; 
    176184         
    177185        address                                 = wi_socket_address(socket); 
     
    180188         
    181189        client->socket_lock             = wi_lock_init(wi_lock_alloc()); 
    182         client->flag_lock              = wi_lock_init(wi_lock_alloc()); 
     190        client->client_lock            = wi_rwlock_init(wi_rwlock_alloc()); 
    183191         
    184192        client->transfers_queue = wi_array_init(wi_array_alloc()); 
     
    192200        wd_client_t                     *client = instance; 
    193201         
     202        wi_release(client->client_lock); 
     203        wi_release(client->socket_lock); 
     204 
    194205        wi_release(client->socket); 
    195206         
     
    204215        wi_release(client->image); 
    205216 
    206         wi_release(client->socket_lock); 
    207         wi_release(client->flag_lock); 
    208  
    209217        wi_release(client->transfers_queue); 
    210218} 
     
    228236 
    229237static wd_uid_t wd_client_uid(void) { 
    230         static wd_uid_t         uid; 
    231  
     238        wd_uid_t                uid; 
     239         
     240        wi_lock_lock(wd_clients_uid_lock); 
     241        wi_hash_rdlock(wd_clients); 
     242         
    232243        if(wi_hash_count(wd_clients) == 0) 
    233                 uid = 0; 
    234  
    235         return ++uid; 
    236 
    237  
    238  
    239  
    240 #pragma mark - 
     244                wd_clients_current_uid = 0; 
     245         
     246        uid = ++wd_clients_current_uid; 
     247 
     248        wi_hash_unlock(wd_clients); 
     249        wi_lock_unlock(wd_clients_uid_lock); 
     250 
     251        return uid; 
     252
     253 
     254 
     255 
     256#pragma mark - 
     257 
     258void wd_client_wrlock_client(wd_client_t *client) { 
     259        wi_rwlock_wrlock(client->client_lock); 
     260
     261 
     262 
     263 
     264void wd_client_rdlock_client(wd_client_t *client) { 
     265        wi_rwlock_rdlock(client->client_lock); 
     266
     267 
     268 
     269 
     270void wd_client_unlock_client(wd_client_t *client) { 
     271        wi_rwlock_unlock(client->client_lock); 
     272
     273 
     274 
     275 
    241276 
    242277void wd_client_lock_socket(wd_client_t *client) { 
  • wired/trunk/wired/clients.h

    r4508 r4738  
    5555        wi_runtime_base_t                                       base; 
    5656         
     57        wi_rwlock_t                                                     *client_lock; 
    5758        wi_lock_t                                                       *socket_lock; 
     59         
    5860        wi_socket_t                                                     *socket; 
    5961         
    60         wi_lock_t                                                       *flag_lock; 
    6162        wd_uid_t                                                        uid; 
    6263        wd_client_state_t                                       state; 
     
    9798wd_client_t *                                                   wd_client_init_with_socket(wd_client_t *, wi_socket_t *); 
    9899 
     100void                                                                    wd_client_wrlock_client(wd_client_t *); 
     101void                                                                    wd_client_rdlock_client(wd_client_t *); 
     102void                                                                    wd_client_unlock_client(wd_client_t *); 
    99103void                                                                    wd_client_lock_socket(wd_client_t *); 
    100104void                                                                    wd_client_unlock_socket(wd_client_t *); 
  • wired/trunk/wired/commands.c

    r4568 r4738  
    323323 
    324324        if(wd_commands[index].activate) { 
     325                wd_client_wrlock_client(client); 
     326                 
    325327                client->idle_time = wi_time_interval(); 
    326328 
     
    330332                        wd_client_broadcast_status(client); 
    331333                } 
     334 
     335                wd_client_unlock_client(client); 
    332336        } 
    333337         
     
    408412        wd_tempban(peer->ip); 
    409413 
    410         wi_lock_lock(peer->flag_lock); 
     414        wd_client_wrlock_client(peer); 
    411415        peer->state = WD_CLIENT_STATE_DISCONNECTED; 
    412         wi_lock_unlock(peer->flag_lock); 
     416        wd_client_unlock_client(peer); 
    413417} 
    414418 
     
    932936 
    933937        icon = wi_string_uint32(WI_ARRAY(arguments, 0)); 
     938         
     939        wd_client_wrlock_client(client); 
    934940 
    935941        /* set icon if changed */ 
     
    952958                } 
    953959        } 
     960 
     961        wd_client_unlock_client(client); 
    954962} 
    955963 
     
    11471155                peer->nick, peer->login, peer->ip); 
    11481156 
    1149         wi_lock_lock(peer->flag_lock); 
     1157        wd_client_wrlock_client(peer); 
    11501158        peer->state = WD_CLIENT_STATE_DISCONNECTED; 
    1151         wi_lock_unlock(peer->flag_lock); 
     1159        wd_client_unlock_client(peer); 
    11521160} 
    11531161 
     
    13451353        nick = WI_ARRAY(arguments, 0); 
    13461354 
     1355        wd_client_wrlock_client(client); 
     1356 
    13471357        if(!wi_is_equal(nick, client->nick)) { 
    13481358                wi_release(client->nick); 
     
    13521362                        wd_client_broadcast_status(client); 
    13531363        } 
     1364 
     1365        wd_client_unlock_client(client); 
    13541366} 
    13551367 
     
    13931405           client->nick, client->login, client->ip); 
    13941406 
    1395         wi_lock_lock(client->flag_lock); 
     1407        wd_client_wrlock_client(client); 
    13961408        client->admin = (client->account->kick_users || client->account->ban_users); 
    13971409        client->state = WD_CLIENT_STATE_LOGGED_IN; 
    1398         wi_lock_unlock(client->flag_lock); 
     1410        wd_client_unlock_client(client); 
    13991411 
    14001412        wi_lock_lock(wd_status_lock); 
     
    16451657 
    16461658        status = WI_ARRAY(arguments, 0); 
     1659         
     1660        wd_client_wrlock_client(client); 
    16471661 
    16481662        if(!wi_is_equal(status, client->status)) { 
     
    16531667                        wd_client_broadcast_status(client); 
    16541668        } 
     1669 
     1670        wd_client_unlock_client(client); 
    16551671} 
    16561672 
  • wired/trunk/wired/files.c

    r4621 r4738  
    10791079        wi_file_t               *file; 
    10801080        wi_string_t             *name, *dirpath, *realdirpath, *commentpath; 
    1081         wi_string_t             *eachname, *eachcomment, *comment = NULL
     1081        wi_string_t             *eachname, *eachcomment
    10821082         
    10831083        name            = wi_string_last_path_component(path); 
     
    10941094 
    10951095        while(wd_files_read_comment(file, &eachname, &eachcomment)) { 
    1096                 if(wi_is_equal(name, eachname)) { 
    1097                         comment = wi_retain(eachcomment); 
    1098                          
    1099                         break; 
    1100                 } 
    1101         } 
    1102  
    1103         return wi_autorelease(comment); 
     1096                if(wi_is_equal(name, eachname)) 
     1097                        return eachcomment; 
     1098        } 
     1099         
     1100        return NULL; 
    11041101} 
    11051102 
     
    12441241        wd_file_type_t  type; 
    12451242        wi_uinteger_t   i, count; 
    1246         wi_boolean_t    result = false; 
    12471243         
    12481244        realpath = wd_files_real_path(WI_STR("/")); 
    12491245        wi_string_resolve_aliases_in_path(realpath); 
    12501246         
    1251         dirpath = wi_copy(path); 
     1247        dirpath = wi_autorelease(wi_copy(path)); 
    12521248        wi_string_delete_last_path_component(dirpath); 
    12531249 
     
    12601256                type = wd_files_type(realpath); 
    12611257 
    1262                 if(type == WD_FILE_TYPE_DROPBOX) { 
    1263                         result = true; 
    1264                          
    1265                         break; 
    1266                 } 
    1267         } 
    1268          
    1269         wi_release(dirpath); 
    1270          
    1271         return result; 
     1258                if(wd_files_type(realpath) == WD_FILE_TYPE_DROPBOX) 
     1259                        return true; 
     1260        } 
     1261         
     1262        return false; 
    12721263} 
    12731264 
  • wired/trunk/wired/server.c

    r4632 r4738  
    482482        va_end(ap); 
    483483         
    484         wi_array_rdlock(chat->clients); 
     484        wd_chat_rdlock(chat); 
    485485 
    486486        enumerator = wi_array_data_enumerator(chat->clients); 
    487487         
    488488        while((client = wi_enumerator_next_data(enumerator))) { 
     489                wd_client_rdlock_client(client); 
     490                 
    489491                if(client->state == WD_CLIENT_STATE_LOGGED_IN) { 
    490492                        wd_client_lock_socket(client); 
     
    492494                        wd_client_unlock_socket(client); 
    493495                } 
    494         } 
    495          
    496         wi_array_unlock(chat->clients); 
     496 
     497                wd_client_unlock_client(client); 
     498        } 
     499         
     500        wd_chat_unlock(chat); 
    497501 
    498502        wi_release(string); 
  • wired/trunk/wired/trackers.c

    r4576 r4738  
    418418static void wd_tracker_update(wd_tracker_t *tracker) { 
    419419        wi_socket_t                     *socket; 
    420         wi_string_t                     *string = NULL; 
    421420        wi_integer_t            bytes; 
    422421        uint32_t                        guest, download; 
     
    425424        download = (guest && wd_trackers_guest_account->download); 
    426425         
    427         socket = wi_socket_init_with_address(wi_socket_alloc(), tracker->address, WI_SOCKET_UDP); 
     426        socket = wi_autorelease(wi_socket_init_with_address(wi_socket_alloc(), tracker->address, WI_SOCKET_UDP)); 
    428427         
    429428        if(!socket) { 
     
    431430                        tracker->host); 
    432431                 
    433                 goto end
     432                return
    434433        } 
    435434         
     
    446445                        tracker->host); 
    447446                 
    448                 goto end; 
    449         } 
    450  
    451 end: 
    452         wi_release(string); 
    453         wi_release(socket); 
     447                return; 
     448        } 
    454449} 
    455450