initialize the locks; return 0 on success, -1 otherwise */
| 76 | /* initialize the locks; return 0 on success, -1 otherwise |
| 77 | */ |
| 78 | int lock_initialize( unsigned int timer_sets ) |
| 79 | { |
| 80 | int i; |
| 81 | #ifndef GEN_LOCK_T_PREFERED |
| 82 | int probe_run; |
| 83 | #endif |
| 84 | |
| 85 | /* first try allocating semaphore sets with fixed number of semaphores */ |
| 86 | LM_DBG("lock initialization started\n"); |
| 87 | |
| 88 | timer_group_lock=shm_malloc(timer_sets*TG_NR*sizeof(ser_lock_t)); |
| 89 | if (timer_group_lock==0){ |
| 90 | LM_CRIT("no more share mem\n"); |
| 91 | goto error; |
| 92 | } |
| 93 | #ifdef GEN_LOCK_T_PREFERED |
| 94 | for(i=0;i<timer_sets*TG_NR;i++) lock_init(&timer_group_lock[i]); |
| 95 | #else |
| 96 | /* transaction timers */ |
| 97 | if (((timer_semaphore= lock_set_alloc( TG_NR ) ) == 0)|| |
| 98 | (lock_set_init(timer_semaphore)==0)){ |
| 99 | if (timer_semaphore) lock_set_destroy(timer_semaphore); |
| 100 | LM_CRIT("transaction timer semaphore initialization failure: %s\n", |
| 101 | strerror(errno)); |
| 102 | goto error; |
| 103 | } |
| 104 | |
| 105 | for (i=0; i<TG_NR; i++) { |
| 106 | timer_group_lock[i].semaphore_set = timer_semaphore; |
| 107 | timer_group_lock[i].semaphore_index = timer_group[ i ]; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | i=SEM_MIN; |
| 112 | /* probing phase: 0=initial, 1=after the first failure */ |
| 113 | probe_run=0; |
| 114 | again: |
| 115 | do { |
| 116 | if (entry_semaphore!=0){ /* clean-up previous attempt */ |
| 117 | lock_set_destroy(entry_semaphore); |
| 118 | lock_set_dealloc(entry_semaphore); |
| 119 | } |
| 120 | if (reply_semaphore!=0){ |
| 121 | lock_set_destroy(reply_semaphore); |
| 122 | lock_set_dealloc(reply_semaphore); |
| 123 | } |
| 124 | |
| 125 | if (i==0){ |
| 126 | LM_CRIT("failed allocate semaphore sets\n"); |
| 127 | goto error; |
| 128 | } |
| 129 | |
| 130 | if (((entry_semaphore=lock_set_alloc(i))==0)|| |
| 131 | (lock_set_init(entry_semaphore)==0)) { |
| 132 | LM_DBG("entry semaphore initialization failure: %s\n", |
| 133 | strerror( errno ) ); |
| 134 | if (entry_semaphore){ |
| 135 | lock_set_dealloc(entry_semaphore); |
no test coverage detected