int ringbuf_push(const char *value, size_t len) { static pid_t pid = 0; if (unlikely(len > shm->size)) return -1; if (unlikely(pid == 0)) pid = getpid(); shmtx_lock(&mutex_w, pid); uint32_t index_w = shm->index_w; uint32_t next = Next_Slot(index_w, shm->max); if (next == shm->index_r) { shmtx_unlock(&mutex_w, pid); return Cache_Full; } memcpy(shm->datas + index_w * shm->size, value, len); atomic_cmp_set(&shm->index_w, index_w, next); if (!shmtx_unlock(&mutex_w, pid)) { #ifdef RB_DEBUG printf("push lock exception! %lu\n", *mutex_w.lock); #endif } return 0; }
int ringbuf_pop(char *value) { static pid_t pid = 0; if (unlikely(pid == 0)) pid = getpid(); shmtx_lock(&mutex_r, pid); uint32_t index_r = shm->index_r; if (shm->index_w == index_r) { shmtx_unlock(&mutex_r, pid); return No_Data; } uint32_t next = Next_Slot(index_r, shm->max); memcpy(value, shm->datas + index_r * shm->size, shm->size); atomic_cmp_set(&shm->index_r, index_r, next); if (!shmtx_unlock(&mutex_r, pid)) { #ifdef RB_DEBUG printf("pop lock exception! %lu \n", *mutex_r.lock); #endif } return 0; }
static void nchan_msg_reserve_debug(nchan_msg_t *msg, char *lbl) { msg_rsv_dbg_t *rsv; int shared = msg->storage == NCHAN_MSG_SHARED; if(shared) shmtx_lock(nchan_store_memory_shmem); if(shared) rsv=shm_locked_calloc(nchan_store_memory_shmem, sizeof(*rsv) + ngx_strlen(lbl) + 1, "msgdebug"); else rsv=ngx_calloc(sizeof(*rsv) + ngx_strlen(lbl) + 1, ngx_cycle->log); rsv->lbl = (char *)(&rsv[1]); ngx_memcpy(rsv->lbl, lbl, ngx_strlen(lbl)); if(msg->rsv == NULL) { msg->rsv = rsv; rsv->prev = NULL; rsv->next = NULL; } else { msg->rsv->prev = rsv; rsv->next = msg->rsv; rsv->prev = NULL; msg->rsv = rsv; } if(shared) shmtx_unlock(nchan_store_memory_shmem); }
static void nchan_msg_release_debug(nchan_msg_t *msg, char *lbl) { msg_rsv_dbg_t *cur, *prev, *next; size_t sz = ngx_strlen(lbl); ngx_int_t rsv_found=0; int shared = msg->storage == NCHAN_MSG_SHARED; if(shared) shmtx_lock(nchan_store_memory_shmem); assert(msg->refcount > 0); for(cur = msg->rsv; cur != NULL; cur = cur->next) { if(ngx_memcmp(lbl, cur->lbl, sz) == 0) { prev = cur->prev; next = cur->next; if(prev) { prev->next = next; } if(next) { next->prev = prev; } if(cur == msg->rsv) { msg->rsv = next; } if(shared) shm_locked_free(nchan_store_memory_shmem, cur); else ngx_free(cur); rsv_found = 1; break; } } assert(rsv_found); if(shared) shmtx_unlock(nchan_store_memory_shmem); }