1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| sr_error_info_t * sr_rwlock_init(sr_rwlock_t *rwlock, int shared) { sr_error_info_t *err_info = NULL;
if ((err_info = sr_mutex_init(&rwlock->mutex, shared))) { return err_info; } rwlock->readers = 0; if ((err_info = sr_cond_init(&rwlock->cond, shared))) { pthread_mutex_destroy(&rwlock->mutex); return err_info; }
return NULL; }
sr_error_info_t * sr_mutex_init(pthread_mutex_t *lock, int shared) { sr_error_info_t *err_info = NULL; pthread_mutexattr_t attr; int ret;
if (SR_MUTEX_ALIGN_CHECK(lock)) { sr_errinfo_new(&err_info, SR_ERR_INTERNAL, NULL, "Mutex address not aligned."); return err_info; }
if (shared) { if ((ret = pthread_mutexattr_init(&attr))) { sr_errinfo_new(&err_info, SR_ERR_SYS, NULL, "Initializing pthread attr failed (%s).", strerror(ret)); return err_info; } if ((ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED))) { pthread_mutexattr_destroy(&attr); sr_errinfo_new(&err_info, SR_ERR_SYS, NULL, "Changing pthread attr failed (%s).", strerror(ret)); return err_info; }
if ((ret = pthread_mutex_init(lock, &attr))) { pthread_mutexattr_destroy(&attr); sr_errinfo_new(&err_info, SR_ERR_SYS, NULL, "Initializing pthread mutex failed (%s).", strerror(ret)); return err_info; } pthread_mutexattr_destroy(&attr); } else { if ((ret = pthread_mutex_init(lock, NULL))) { sr_errinfo_new(&err_info, SR_ERR_SYS, NULL, "Initializing pthread mutex failed (%s).", strerror(ret)); return err_info; } }
return NULL; }
static sr_error_info_t * sr_cond_init(pthread_cond_t *cond, int shared) { sr_error_info_t *err_info = NULL; pthread_condattr_t attr; int ret;
if (SR_COND_ALIGN_CHECK(cond)) { sr_errinfo_new(&err_info, SR_ERR_INTERNAL, NULL, "Condition variable address not aligned."); return err_info; }
if (shared) { if ((ret = pthread_condattr_init(&attr))) { sr_errinfo_new(&err_info, SR_ERR_SYS, NULL, "Initializing pthread attr failed (%s).", strerror(ret)); return err_info; } if ((ret = pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED))) { pthread_condattr_destroy(&attr); sr_errinfo_new(&err_info, SR_ERR_SYS, NULL, "Changing pthread attr failed (%s).", strerror(ret)); return err_info; }
if ((ret = pthread_cond_init(cond, &attr))) { pthread_condattr_destroy(&attr); sr_errinfo_new(&err_info, SR_ERR_SYS, NULL, "Initializing pthread rwlock failed (%s).", strerror(ret)); return err_info; } pthread_condattr_destroy(&attr); } else { if ((ret = pthread_cond_init(cond, NULL))) { sr_errinfo_new(&err_info, SR_ERR_SYS, NULL, "Initializing pthread rwlock failed (%s).", strerror(ret)); return err_info; } }
return NULL; }
|