| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <pj/lock.h> |
| 20 |
#include <pj/os.h> |
| 21 |
#include <pj/assert.h> |
| 22 |
#include <pj/log.h> |
| 23 |
#include <pj/pool.h> |
| 24 |
#include <pj/string.h> |
| 25 |
#include <pj/errno.h> |
| 26 |
|
| 27 |
#define THIS_FILE "lock.c" |
| 28 |
|
| 29 |
typedef void LOCK_OBJ; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
struct pj_lock_t |
| 35 |
{ |
| 36 |
LOCK_OBJ *lock_object; |
| 37 |
|
| 38 |
pj_status_t (*acquire) (LOCK_OBJ*); |
| 39 |
pj_status_t (*tryacquire) (LOCK_OBJ*); |
| 40 |
pj_status_t (*release) (LOCK_OBJ*); |
| 41 |
pj_status_t (*destroy) (LOCK_OBJ*); |
| 42 |
}; |
| 43 |
|
| 44 |
typedef pj_status_t (*FPTR)(LOCK_OBJ*); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static pj_lock_t mutex_lock_template = |
| 50 |
{ |
| 51 |
NULL, |
| 52 |
(FPTR) &pj_mutex_lock, |
| 53 |
(FPTR) &pj_mutex_trylock, |
| 54 |
(FPTR) &pj_mutex_unlock, |
| 55 |
(FPTR) &pj_mutex_destroy |
| 56 |
}; |
| 57 |
|
| 58 |
static pj_status_t create_mutex_lock( pj_pool_t *pool, |
| 59 |
const char *name, |
| 60 |
int type, |
| 61 |
pj_lock_t **lock ) |
| 62 |
{ |
| 63 |
pj_lock_t *p_lock; |
| 64 |
pj_mutex_t *mutex; |
| 65 |
pj_status_t rc; |
| 66 |
|
| 67 |
PJ_ASSERT_RETURN(pool && lock, PJ_EINVAL); |
| 68 |
|
| 69 |
p_lock = PJ_POOL_ALLOC_T(pool, pj_lock_t); |
| 70 |
if (!p_lock) |
| 71 |
return PJ_ENOMEM; |
| 72 |
|
| 73 |
pj_memcpy(p_lock, &mutex_lock_template, sizeof(pj_lock_t)); |
| 74 |
rc = pj_mutex_create(pool, name, type, &mutex); |
| 75 |
if (rc != PJ_SUCCESS) |
| 76 |
return rc; |
| 77 |
|
| 78 |
p_lock->lock_object = mutex; |
| 79 |
*lock = p_lock; |
| 80 |
return PJ_SUCCESS; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
PJ_DEF(pj_status_t) pj_lock_create_simple_mutex( pj_pool_t *pool, |
| 85 |
const char *name, |
| 86 |
pj_lock_t **lock ) |
| 87 |
{ |
| 88 |
return create_mutex_lock(pool, name, PJ_MUTEX_SIMPLE, lock); |
| 89 |
} |
| 90 |
|
| 91 |
PJ_DEF(pj_status_t) pj_lock_create_recursive_mutex( pj_pool_t *pool, |
| 92 |
const char *name, |
| 93 |
pj_lock_t **lock ) |
| 94 |
{ |
| 95 |
return create_mutex_lock(pool, name, PJ_MUTEX_RECURSE, lock); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static pj_status_t null_op(void *arg) |
| 103 |
{ |
| 104 |
PJ_UNUSED_ARG(arg); |
| 105 |
return PJ_SUCCESS; |
| 106 |
} |
| 107 |
|
| 108 |
static pj_lock_t null_lock_template = |
| 109 |
{ |
| 110 |
NULL, |
| 111 |
&null_op, |
| 112 |
&null_op, |
| 113 |
&null_op, |
| 114 |
&null_op |
| 115 |
}; |
| 116 |
|
| 117 |
PJ_DEF(pj_status_t) pj_lock_create_null_mutex( pj_pool_t *pool, |
| 118 |
const char *name, |
| 119 |
pj_lock_t **lock ) |
| 120 |
{ |
| 121 |
PJ_UNUSED_ARG(name); |
| 122 |
PJ_UNUSED_ARG(pool); |
| 123 |
|
| 124 |
PJ_ASSERT_RETURN(lock, PJ_EINVAL); |
| 125 |
|
| 126 |
*lock = &null_lock_template; |
| 127 |
return PJ_SUCCESS; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#if defined(PJ_HAS_SEMAPHORE) && PJ_HAS_SEMAPHORE != 0 |
| 135 |
|
| 136 |
static pj_lock_t sem_lock_template = |
| 137 |
{ |
| 138 |
NULL, |
| 139 |
(FPTR) &pj_sem_wait, |
| 140 |
(FPTR) &pj_sem_trywait, |
| 141 |
(FPTR) &pj_sem_post, |
| 142 |
(FPTR) &pj_sem_destroy |
| 143 |
}; |
| 144 |
|
| 145 |
PJ_DEF(pj_status_t) pj_lock_create_semaphore( pj_pool_t *pool, |
| 146 |
const char *name, |
| 147 |
unsigned initial, |
| 148 |
unsigned max, |
| 149 |
pj_lock_t **lock ) |
| 150 |
{ |
| 151 |
pj_lock_t *p_lock; |
| 152 |
pj_sem_t *sem; |
| 153 |
pj_status_t rc; |
| 154 |
|
| 155 |
PJ_ASSERT_RETURN(pool && lock, PJ_EINVAL); |
| 156 |
|
| 157 |
p_lock = PJ_POOL_ALLOC_T(pool, pj_lock_t); |
| 158 |
if (!p_lock) |
| 159 |
return PJ_ENOMEM; |
| 160 |
|
| 161 |
pj_memcpy(p_lock, &sem_lock_template, sizeof(pj_lock_t)); |
| 162 |
rc = pj_sem_create( pool, name, initial, max, &sem); |
| 163 |
if (rc != PJ_SUCCESS) |
| 164 |
return rc; |
| 165 |
|
| 166 |
p_lock->lock_object = sem; |
| 167 |
*lock = p_lock; |
| 168 |
|
| 169 |
return PJ_SUCCESS; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
#endif |
| 174 |
|
| 175 |
|
| 176 |
PJ_DEF(pj_status_t) pj_lock_acquire( pj_lock_t *lock ) |
| 177 |
{ |
| 178 |
PJ_ASSERT_RETURN(lock != NULL, PJ_EINVAL); |
| 179 |
return (*lock->acquire)(lock->lock_object); |
| 180 |
} |
| 181 |
|
| 182 |
PJ_DEF(pj_status_t) pj_lock_tryacquire( pj_lock_t *lock ) |
| 183 |
{ |
| 184 |
PJ_ASSERT_RETURN(lock != NULL, PJ_EINVAL); |
| 185 |
return (*lock->tryacquire)(lock->lock_object); |
| 186 |
} |
| 187 |
|
| 188 |
PJ_DEF(pj_status_t) pj_lock_release( pj_lock_t *lock ) |
| 189 |
{ |
| 190 |
PJ_ASSERT_RETURN(lock != NULL, PJ_EINVAL); |
| 191 |
return (*lock->release)(lock->lock_object); |
| 192 |
} |
| 193 |
|
| 194 |
PJ_DEF(pj_status_t) pj_lock_destroy( pj_lock_t *lock ) |
| 195 |
{ |
| 196 |
PJ_ASSERT_RETURN(lock != NULL, PJ_EINVAL); |
| 197 |
return (*lock->destroy)(lock->lock_object); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
typedef struct grp_lock_item |
| 207 |
{ |
| 208 |
PJ_DECL_LIST_MEMBER(struct grp_lock_item); |
| 209 |
int prio; |
| 210 |
pj_lock_t *lock; |
| 211 |
|
| 212 |
} grp_lock_item; |
| 213 |
|
| 214 |
|
| 215 |
typedef struct grp_destroy_callback |
| 216 |
{ |
| 217 |
PJ_DECL_LIST_MEMBER(struct grp_destroy_callback); |
| 218 |
void *comp; |
| 219 |
void (*handler)(void*); |
| 220 |
} grp_destroy_callback; |
| 221 |
|
| 222 |
#if PJ_GRP_LOCK_DEBUG |
| 223 |
|
| 224 |
typedef struct grp_lock_ref |
| 225 |
{ |
| 226 |
PJ_DECL_LIST_MEMBER(struct grp_lock_ref); |
| 227 |
const char *file; |
| 228 |
int line; |
| 229 |
} grp_lock_ref; |
| 230 |
#endif |
| 231 |
|
| 232 |
|
| 233 |
struct pj_grp_lock_t |
| 234 |
{ |
| 235 |
pj_lock_t base; |
| 236 |
|
| 237 |
pj_pool_t *pool; |
| 238 |
pj_atomic_t *ref_cnt; |
| 239 |
pj_lock_t *own_lock; |
| 240 |
|
| 241 |
pj_thread_t *owner; |
| 242 |
int owner_cnt; |
| 243 |
|
| 244 |
grp_lock_item lock_list; |
| 245 |
grp_destroy_callback destroy_list; |
| 246 |
|
| 247 |
#if PJ_GRP_LOCK_DEBUG |
| 248 |
grp_lock_ref ref_list; |
| 249 |
grp_lock_ref ref_free_list; |
| 250 |
#endif |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
PJ_DEF(void) pj_grp_lock_config_default(pj_grp_lock_config *cfg) |
| 255 |
{ |
| 256 |
pj_bzero(cfg, sizeof(*cfg)); |
| 257 |
} |
| 258 |
|
| 259 |
static void grp_lock_set_owner_thread(pj_grp_lock_t *glock) |
| 260 |
{ |
| 261 |
if (!glock->owner) { |
| 262 |
#if PJ_HAS_THREADS |
| 263 |
glock->owner = pj_thread_this(); |
| 264 |
#else |
| 265 |
glock->owner = (pj_thread_t *) -1; |
| 266 |
#endif |
| 267 |
glock->owner_cnt = 1; |
| 268 |
} else { |
| 269 |
#if PJ_HAS_THREADS |
| 270 |
pj_assert(glock->owner == pj_thread_this()); |
| 271 |
#endif |
| 272 |
glock->owner_cnt++; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static void grp_lock_unset_owner_thread(pj_grp_lock_t *glock) |
| 277 |
{ |
| 278 |
#if PJ_HAS_THREADS |
| 279 |
pj_assert(glock->owner == pj_thread_this()); |
| 280 |
#endif |
| 281 |
pj_assert(glock->owner_cnt > 0); |
| 282 |
if (--glock->owner_cnt <= 0) { |
| 283 |
glock->owner = NULL; |
| 284 |
glock->owner_cnt = 0; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
static pj_status_t grp_lock_acquire(LOCK_OBJ *p) |
| 289 |
{ |
| 290 |
pj_grp_lock_t *glock = (pj_grp_lock_t*)p; |
| 291 |
grp_lock_item *lck; |
| 292 |
|
| 293 |
pj_assert(pj_atomic_get(glock->ref_cnt) > 0); |
| 294 |
|
| 295 |
lck = glock->lock_list.next; |
| 296 |
while (lck != &glock->lock_list) { |
| 297 |
pj_lock_acquire(lck->lock); |
| 298 |
lck = lck->next; |
| 299 |
} |
| 300 |
grp_lock_set_owner_thread(glock); |
| 301 |
pj_grp_lock_add_ref(glock); |
| 302 |
return PJ_SUCCESS; |
| 303 |
} |
| 304 |
|
| 305 |
static pj_status_t grp_lock_tryacquire(LOCK_OBJ *p) |
| 306 |
{ |
| 307 |
pj_grp_lock_t *glock = (pj_grp_lock_t*)p; |
| 308 |
grp_lock_item *lck; |
| 309 |
|
| 310 |
pj_assert(pj_atomic_get(glock->ref_cnt) > 0); |
| 311 |
|
| 312 |
lck = glock->lock_list.next; |
| 313 |
while (lck != &glock->lock_list) { |
| 314 |
pj_status_t status = pj_lock_tryacquire(lck->lock); |
| 315 |
if (status != PJ_SUCCESS) { |
| 316 |
lck = lck->prev; |
| 317 |
while (lck != &glock->lock_list) { |
| 318 |
pj_lock_release(lck->lock); |
| 319 |
lck = lck->prev; |
| 320 |
} |
| 321 |
return status; |
| 322 |
} |
| 323 |
lck = lck->next; |
| 324 |
} |
| 325 |
grp_lock_set_owner_thread(glock); |
| 326 |
pj_grp_lock_add_ref(glock); |
| 327 |
return PJ_SUCCESS; |
| 328 |
} |
| 329 |
|
| 330 |
static pj_status_t grp_lock_release(LOCK_OBJ *p) |
| 331 |
{ |
| 332 |
pj_grp_lock_t *glock = (pj_grp_lock_t*)p; |
| 333 |
grp_lock_item *lck; |
| 334 |
|
| 335 |
grp_lock_unset_owner_thread(glock); |
| 336 |
|
| 337 |
lck = glock->lock_list.prev; |
| 338 |
while (lck != &glock->lock_list) { |
| 339 |
pj_lock_release(lck->lock); |
| 340 |
lck = lck->prev; |
| 341 |
} |
| 342 |
return pj_grp_lock_dec_ref(glock); |
| 343 |
} |
| 344 |
|
| 345 |
static pj_status_t grp_lock_add_handler( pj_grp_lock_t *glock, |
| 346 |
pj_pool_t *pool, |
| 347 |
void *comp, |
| 348 |
void (*destroy)(void *comp), |
| 349 |
pj_bool_t acquire_lock) |
| 350 |
{ |
| 351 |
grp_destroy_callback *cb; |
| 352 |
|
| 353 |
if (acquire_lock) |
| 354 |
grp_lock_acquire(glock); |
| 355 |
|
| 356 |
if (pool == NULL) |
| 357 |
pool = glock->pool; |
| 358 |
|
| 359 |
cb = PJ_POOL_ZALLOC_T(pool, grp_destroy_callback); |
| 360 |
cb->comp = comp; |
| 361 |
cb->handler = destroy; |
| 362 |
pj_list_push_back(&glock->destroy_list, cb); |
| 363 |
|
| 364 |
if (acquire_lock) |
| 365 |
grp_lock_release(glock); |
| 366 |
|
| 367 |
return PJ_SUCCESS; |
| 368 |
} |
| 369 |
|
| 370 |
static pj_status_t grp_lock_destroy(LOCK_OBJ *p) |
| 371 |
{ |
| 372 |
pj_grp_lock_t *glock = (pj_grp_lock_t*)p; |
| 373 |
pj_pool_t *pool = glock->pool; |
| 374 |
grp_lock_item *lck; |
| 375 |
grp_destroy_callback *cb; |
| 376 |
|
| 377 |
if (!glock->pool) { |
| 378 |
|
| 379 |
return PJ_EINVAL; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
lck = glock->lock_list.next; |
| 384 |
while (lck != &glock->lock_list) { |
| 385 |
if (lck->lock != glock->own_lock) { |
| 386 |
int i; |
| 387 |
for (i=0; i<glock->owner_cnt; ++i) |
| 388 |
pj_lock_release(lck->lock); |
| 389 |
} |
| 390 |
lck = lck->next; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
cb = glock->destroy_list.next; |
| 395 |
while (cb != &glock->destroy_list) { |
| 396 |
grp_destroy_callback *next = cb->next; |
| 397 |
cb->handler(cb->comp); |
| 398 |
cb = next; |
| 399 |
} |
| 400 |
|
| 401 |
pj_lock_destroy(glock->own_lock); |
| 402 |
pj_atomic_destroy(glock->ref_cnt); |
| 403 |
glock->pool = NULL; |
| 404 |
pj_pool_release(pool); |
| 405 |
|
| 406 |
return PJ_SUCCESS; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
PJ_DEF(pj_status_t) pj_grp_lock_create( pj_pool_t *pool, |
| 411 |
const pj_grp_lock_config *cfg, |
| 412 |
pj_grp_lock_t **p_grp_lock) |
| 413 |
{ |
| 414 |
pj_grp_lock_t *glock; |
| 415 |
grp_lock_item *own_lock; |
| 416 |
pj_status_t status; |
| 417 |
|
| 418 |
PJ_ASSERT_RETURN(pool && p_grp_lock, PJ_EINVAL); |
| 419 |
|
| 420 |
PJ_UNUSED_ARG(cfg); |
| 421 |
|
| 422 |
pool = pj_pool_create(pool->factory, "glck%p", 512, 512, NULL); |
| 423 |
if (!pool) |
| 424 |
return PJ_ENOMEM; |
| 425 |
|
| 426 |
glock = PJ_POOL_ZALLOC_T(pool, pj_grp_lock_t); |
| 427 |
glock->base.lock_object = glock; |
| 428 |
glock->base.acquire = &grp_lock_acquire; |
| 429 |
glock->base.tryacquire = &grp_lock_tryacquire; |
| 430 |
glock->base.release = &grp_lock_release; |
| 431 |
glock->base.destroy = &grp_lock_destroy; |
| 432 |
|
| 433 |
glock->pool = pool; |
| 434 |
pj_list_init(&glock->lock_list); |
| 435 |
pj_list_init(&glock->destroy_list); |
| 436 |
#if PJ_GRP_LOCK_DEBUG |
| 437 |
pj_list_init(&glock->ref_list); |
| 438 |
pj_list_init(&glock->ref_free_list); |
| 439 |
#endif |
| 440 |
|
| 441 |
status = pj_atomic_create(pool, 0, &glock->ref_cnt); |
| 442 |
if (status != PJ_SUCCESS) |
| 443 |
goto on_error; |
| 444 |
|
| 445 |
status = pj_lock_create_recursive_mutex(pool, pool->obj_name, |
| 446 |
&glock->own_lock); |
| 447 |
if (status != PJ_SUCCESS) |
| 448 |
goto on_error; |
| 449 |
|
| 450 |
own_lock = PJ_POOL_ZALLOC_T(pool, grp_lock_item); |
| 451 |
own_lock->lock = glock->own_lock; |
| 452 |
pj_list_push_back(&glock->lock_list, own_lock); |
| 453 |
|
| 454 |
*p_grp_lock = glock; |
| 455 |
return PJ_SUCCESS; |
| 456 |
|
| 457 |
on_error: |
| 458 |
grp_lock_destroy(glock); |
| 459 |
return status; |
| 460 |
} |
| 461 |
|
| 462 |
PJ_DEF(pj_status_t) pj_grp_lock_create_w_handler( pj_pool_t *pool, |
| 463 |
const pj_grp_lock_config *cfg, |
| 464 |
void *member, |
| 465 |
void (*handler)(void *member), |
| 466 |
pj_grp_lock_t **p_grp_lock) |
| 467 |
{ |
| 468 |
pj_status_t status; |
| 469 |
|
| 470 |
status = pj_grp_lock_create(pool, cfg, p_grp_lock); |
| 471 |
if (status == PJ_SUCCESS) { |
| 472 |
pj_pool_t *grppool = (*p_grp_lock)->pool; |
| 473 |
grp_lock_add_handler(*p_grp_lock, grppool, member, handler, PJ_FALSE); |
| 474 |
} |
| 475 |
|
| 476 |
return status; |
| 477 |
} |
| 478 |
|
| 479 |
PJ_DEF(pj_status_t) pj_grp_lock_destroy( pj_grp_lock_t *grp_lock) |
| 480 |
{ |
| 481 |
return grp_lock_destroy(grp_lock); |
| 482 |
} |
| 483 |
|
| 484 |
PJ_DEF(pj_status_t) pj_grp_lock_acquire( pj_grp_lock_t *grp_lock) |
| 485 |
{ |
| 486 |
return grp_lock_acquire(grp_lock); |
| 487 |
} |
| 488 |
|
| 489 |
PJ_DEF(pj_status_t) pj_grp_lock_tryacquire( pj_grp_lock_t *grp_lock) |
| 490 |
{ |
| 491 |
return grp_lock_tryacquire(grp_lock); |
| 492 |
} |
| 493 |
|
| 494 |
PJ_DEF(pj_status_t) pj_grp_lock_release( pj_grp_lock_t *grp_lock) |
| 495 |
{ |
| 496 |
return grp_lock_release(grp_lock); |
| 497 |
} |
| 498 |
|
| 499 |
PJ_DEF(pj_status_t) pj_grp_lock_replace( pj_grp_lock_t *old_lock, |
| 500 |
pj_grp_lock_t *new_lock) |
| 501 |
{ |
| 502 |
grp_destroy_callback *ocb; |
| 503 |
|
| 504 |
|
| 505 |
ocb = old_lock->destroy_list.next; |
| 506 |
while (ocb != &old_lock->destroy_list) { |
| 507 |
grp_destroy_callback *ncb; |
| 508 |
|
| 509 |
ncb = PJ_POOL_ALLOC_T(new_lock->pool, grp_destroy_callback); |
| 510 |
ncb->comp = ocb->comp; |
| 511 |
ncb->handler = ocb->handler; |
| 512 |
pj_list_push_back(&new_lock->destroy_list, ncb); |
| 513 |
|
| 514 |
ocb = ocb->next; |
| 515 |
} |
| 516 |
|
| 517 |
pj_list_init(&old_lock->destroy_list); |
| 518 |
|
| 519 |
grp_lock_destroy(old_lock); |
| 520 |
return PJ_SUCCESS; |
| 521 |
} |
| 522 |
|
| 523 |
PJ_DEF(pj_status_t) pj_grp_lock_add_handler( pj_grp_lock_t *glock, |
| 524 |
pj_pool_t *pool, |
| 525 |
void *comp, |
| 526 |
void (*destroy)(void *comp)) |
| 527 |
{ |
| 528 |
return grp_lock_add_handler(glock, pool, comp, destroy, PJ_TRUE); |
| 529 |
} |
| 530 |
|
| 531 |
PJ_DEF(pj_status_t) pj_grp_lock_del_handler( pj_grp_lock_t *glock, |
| 532 |
void *comp, |
| 533 |
void (*destroy)(void *comp)) |
| 534 |
{ |
| 535 |
grp_destroy_callback *cb; |
| 536 |
|
| 537 |
grp_lock_acquire(glock); |
| 538 |
|
| 539 |
cb = glock->destroy_list.next; |
| 540 |
while (cb != &glock->destroy_list) { |
| 541 |
if (cb->comp == comp && cb->handler == destroy) |
| 542 |
break; |
| 543 |
cb = cb->next; |
| 544 |
} |
| 545 |
|
| 546 |
if (cb != &glock->destroy_list) |
| 547 |
pj_list_erase(cb); |
| 548 |
|
| 549 |
grp_lock_release(glock); |
| 550 |
return PJ_SUCCESS; |
| 551 |
} |
| 552 |
|
| 553 |
static pj_status_t grp_lock_add_ref(pj_grp_lock_t *glock) |
| 554 |
{ |
| 555 |
pj_atomic_inc(glock->ref_cnt); |
| 556 |
return PJ_SUCCESS; |
| 557 |
} |
| 558 |
|
| 559 |
static pj_status_t grp_lock_dec_ref(pj_grp_lock_t *glock) |
| 560 |
{ |
| 561 |
int cnt; |
| 562 |
if ((cnt=pj_atomic_dec_and_get(glock->ref_cnt)) == 0) { |
| 563 |
grp_lock_destroy(glock); |
| 564 |
return PJ_EGONE; |
| 565 |
} |
| 566 |
pj_assert(cnt > 0); |
| 567 |
return PJ_SUCCESS; |
| 568 |
} |
| 569 |
|
| 570 |
#if PJ_GRP_LOCK_DEBUG |
| 571 |
static pj_status_t grp_lock_dec_ref_dump(pj_grp_lock_t *glock) |
| 572 |
{ |
| 573 |
pj_status_t status; |
| 574 |
|
| 575 |
status = grp_lock_dec_ref(glock); |
| 576 |
if (status == PJ_SUCCESS) { |
| 577 |
pj_grp_lock_dump(glock); |
| 578 |
} else if (status == PJ_EGONE) { |
| 579 |
PJ_LOG(4,(THIS_FILE, "Group lock %p destroyed.", glock)); |
| 580 |
} |
| 581 |
|
| 582 |
return status; |
| 583 |
} |
| 584 |
|
| 585 |
PJ_DEF(pj_status_t) pj_grp_lock_add_ref_dbg(pj_grp_lock_t *glock, |
| 586 |
const char *file, |
| 587 |
int line) |
| 588 |
{ |
| 589 |
grp_lock_ref *ref; |
| 590 |
pj_status_t status; |
| 591 |
|
| 592 |
pj_enter_critical_section(); |
| 593 |
if (!pj_list_empty(&glock->ref_free_list)) { |
| 594 |
ref = glock->ref_free_list.next; |
| 595 |
pj_list_erase(ref); |
| 596 |
} else { |
| 597 |
ref = PJ_POOL_ALLOC_T(glock->pool, grp_lock_ref); |
| 598 |
} |
| 599 |
|
| 600 |
ref->file = file; |
| 601 |
ref->line = line; |
| 602 |
pj_list_push_back(&glock->ref_list, ref); |
| 603 |
|
| 604 |
pj_leave_critical_section(); |
| 605 |
|
| 606 |
status = grp_lock_add_ref(glock); |
| 607 |
|
| 608 |
if (status != PJ_SUCCESS) { |
| 609 |
pj_enter_critical_section(); |
| 610 |
pj_list_erase(ref); |
| 611 |
pj_list_push_back(&glock->ref_free_list, ref); |
| 612 |
pj_leave_critical_section(); |
| 613 |
} |
| 614 |
|
| 615 |
return status; |
| 616 |
} |
| 617 |
|
| 618 |
PJ_DEF(pj_status_t) pj_grp_lock_dec_ref_dbg(pj_grp_lock_t *glock, |
| 619 |
const char *file, |
| 620 |
int line) |
| 621 |
{ |
| 622 |
grp_lock_ref *ref; |
| 623 |
|
| 624 |
PJ_UNUSED_ARG(line); |
| 625 |
|
| 626 |
pj_enter_critical_section(); |
| 627 |
|
| 628 |
ref = glock->ref_list.next; |
| 629 |
while (ref != &glock->ref_list) { |
| 630 |
if (strcmp(ref->file, file) == 0) { |
| 631 |
pj_list_erase(ref); |
| 632 |
pj_list_push_back(&glock->ref_free_list, ref); |
| 633 |
break; |
| 634 |
} |
| 635 |
ref = ref->next; |
| 636 |
} |
| 637 |
pj_leave_critical_section(); |
| 638 |
|
| 639 |
if (ref == &glock->ref_list) { |
| 640 |
PJ_LOG(2,(THIS_FILE, "pj_grp_lock_dec_ref_dbg() could not find " |
| 641 |
"matching ref for %s", file)); |
| 642 |
} |
| 643 |
|
| 644 |
return grp_lock_dec_ref_dump(glock); |
| 645 |
} |
| 646 |
#else |
| 647 |
PJ_DEF(pj_status_t) pj_grp_lock_add_ref(pj_grp_lock_t *glock) |
| 648 |
{ |
| 649 |
return grp_lock_add_ref(glock); |
| 650 |
} |
| 651 |
|
| 652 |
PJ_DEF(pj_status_t) pj_grp_lock_dec_ref(pj_grp_lock_t *glock) |
| 653 |
{ |
| 654 |
return grp_lock_dec_ref(glock); |
| 655 |
} |
| 656 |
#endif |
| 657 |
|
| 658 |
PJ_DEF(int) pj_grp_lock_get_ref(pj_grp_lock_t *glock) |
| 659 |
{ |
| 660 |
return pj_atomic_get(glock->ref_cnt); |
| 661 |
} |
| 662 |
|
| 663 |
PJ_DEF(pj_status_t) pj_grp_lock_chain_lock( pj_grp_lock_t *glock, |
| 664 |
pj_lock_t *lock, |
| 665 |
int pos) |
| 666 |
{ |
| 667 |
grp_lock_item *lck, *new_lck; |
| 668 |
int i; |
| 669 |
|
| 670 |
grp_lock_acquire(glock); |
| 671 |
|
| 672 |
for (i=0; i<glock->owner_cnt; ++i) |
| 673 |
pj_lock_acquire(lock); |
| 674 |
|
| 675 |
lck = glock->lock_list.next; |
| 676 |
while (lck != &glock->lock_list) { |
| 677 |
if (lck->prio >= pos) |
| 678 |
break; |
| 679 |
lck = lck->next; |
| 680 |
} |
| 681 |
|
| 682 |
new_lck = PJ_POOL_ZALLOC_T(glock->pool, grp_lock_item); |
| 683 |
new_lck->prio = pos; |
| 684 |
new_lck->lock = lock; |
| 685 |
pj_list_insert_before(lck, new_lck); |
| 686 |
|
| 687 |
|
| 688 |
grp_lock_release(glock); |
| 689 |
return PJ_SUCCESS; |
| 690 |
} |
| 691 |
|
| 692 |
PJ_DEF(pj_status_t) pj_grp_lock_unchain_lock( pj_grp_lock_t *glock, |
| 693 |
pj_lock_t *lock) |
| 694 |
{ |
| 695 |
grp_lock_item *lck; |
| 696 |
|
| 697 |
grp_lock_acquire(glock); |
| 698 |
|
| 699 |
lck = glock->lock_list.next; |
| 700 |
while (lck != &glock->lock_list) { |
| 701 |
if (lck->lock == lock) |
| 702 |
break; |
| 703 |
lck = lck->next; |
| 704 |
} |
| 705 |
|
| 706 |
if (lck != &glock->lock_list) { |
| 707 |
int i; |
| 708 |
|
| 709 |
pj_list_erase(lck); |
| 710 |
for (i=0; i<glock->owner_cnt; ++i) |
| 711 |
pj_lock_release(lck->lock); |
| 712 |
} |
| 713 |
|
| 714 |
grp_lock_release(glock); |
| 715 |
return PJ_SUCCESS; |
| 716 |
} |
| 717 |
|
| 718 |
PJ_DEF(void) pj_grp_lock_dump(pj_grp_lock_t *grp_lock) |
| 719 |
{ |
| 720 |
#if PJ_GRP_LOCK_DEBUG |
| 721 |
grp_lock_ref *ref; |
| 722 |
char info_buf[1000]; |
| 723 |
pj_str_t info; |
| 724 |
int null_char_pos; |
| 725 |
|
| 726 |
info.ptr = info_buf; |
| 727 |
info.slen = 0; |
| 728 |
|
| 729 |
grp_lock_add_ref(grp_lock); |
| 730 |
pj_enter_critical_section(); |
| 731 |
|
| 732 |
ref = grp_lock->ref_list.next; |
| 733 |
while (ref != &grp_lock->ref_list && info.slen < sizeof(info_buf)) { |
| 734 |
char *start = info.ptr + info.slen; |
| 735 |
int max_len = sizeof(info_buf) - info.slen; |
| 736 |
int len; |
| 737 |
|
| 738 |
len = pj_ansi_snprintf(start, max_len, "\t%s:%d\n", ref->file, ref->line); |
| 739 |
if (len < 1 || len >= max_len) { |
| 740 |
len = strlen(ref->file); |
| 741 |
if (len > max_len - 1) |
| 742 |
len = max_len - 1; |
| 743 |
|
| 744 |
memcpy(start, ref->file, len); |
| 745 |
start[len++] = '\n'; |
| 746 |
} |
| 747 |
|
| 748 |
info.slen += len; |
| 749 |
|
| 750 |
ref = ref->next; |
| 751 |
} |
| 752 |
|
| 753 |
if (ref != &grp_lock->ref_list) { |
| 754 |
int i; |
| 755 |
for (i=0; i<4; ++i) |
| 756 |
info_buf[sizeof(info_buf)-i-1] = '.'; |
| 757 |
} |
| 758 |
null_char_pos = info.slen > 0 ? info.slen-1 : 0; |
| 759 |
info.ptr[null_char_pos] = '\0'; |
| 760 |
|
| 761 |
pj_leave_critical_section(); |
| 762 |
|
| 763 |
PJ_LOG(4,(THIS_FILE, "Group lock %p, ref_cnt=%d. Reference holders:\n%s", |
| 764 |
grp_lock, pj_grp_lock_get_ref(grp_lock)-1, info.ptr)); |
| 765 |
|
| 766 |
grp_lock_dec_ref(grp_lock); |
| 767 |
#else |
| 768 |
PJ_LOG(4,(THIS_FILE, "Group lock %p, ref_cnt=%d.", |
| 769 |
grp_lock, pj_grp_lock_get_ref(grp_lock))); |
| 770 |
#endif |
| 771 |
} |