void log_event(char *message) { FILE *file; struct tm *current_info; time_t current; char times[50]; int filedes; time(¤t); current_info = localtime(¤t); current = mktime(current_info); strftime(times,25,"%b %d %Y %H:%M",localtime(¤t)); filedes = open("event.log", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if (filedes < 0) { fprintf(stderr, "Cannot Get Event Log File Descriptor\n"); exit(EXIT_FAILURE); } if(get_lock(filedes) < 0) { fprintf(stderr, "Cannot Obtain Event Log File Lock\n"); exit(EXIT_FAILURE); } file = fdopen(filedes, "a"); fprintf(file, "%s %s\n", times,message); release_lock(filedes); fclose(file); close(filedes); }
/*---------------------------------------------------------------------------*/ DLL_EXPORT int hao_initialize(void) { int i = 0; initialize_lock(&ao_lock); /* serialize */ obtain_lock(&ao_lock); /* initialize variables */ for(i = 0; i < HAO_MAXRULE; i++) { ao_cmd[i] = NULL; ao_tgt[i] = NULL; } /* initialize message buffer */ memset(ao_msgbuf, 0, sizeof(ao_msgbuf)); /* Start message monitoring thread */ if ( create_thread (&sysblk.haotid, JOINABLE, hao_thread, NULL, "hao_thread") ) { i = FALSE; } else i = TRUE; release_lock(&ao_lock); return(i); }
/*---------------------------------------------------------------------------*/ static void hao_clear(void) { int i; /* serialize */ obtain_lock(&ao_lock); /* clear all defined rules */ for(i = 0; i < HAO_MAXRULE; i++) { if(ao_tgt[i]) { free(ao_tgt[i]); ao_tgt[i] = NULL; regfree(&ao_preg[i]); } if(ao_cmd[i]) { free(ao_cmd[i]); ao_cmd[i] = NULL; } } release_lock(&ao_lock); logmsg(HHCAO022I); }
/*************************************************************************** Function: sys_sem_create Description: Parameters: Returns: boolean indicating success or failure ***************************************************************************/ mrapi_boolean_t sys_sem_create(int key,int num_locks,int* id) { int i; /* critical section: We don't want anyone else creating/deleting while we are creating */ acquire_lock(&mrapi_db->global_lock); /* first make sure it doesn't already exist */ for (i = 0; i < MRAPI_MAX_SEMS; i++) { if ((mrapi_db->sys_sems[i].key == key) && (mrapi_db->sys_sems[i].valid)) { break; } } if (i == MRAPI_MAX_SEMS) { /* we didn't find it so create it */ for (i = 0; i < MRAPI_MAX_SEMS; i++) { if (!mrapi_db->sys_sems[i].valid) { memset(&mrapi_db->sys_sems[i],0,sizeof(mrapi_sys_sem_t)); mrapi_db->sys_sems[i].valid = MRAPI_TRUE; mrapi_db->sys_sems[i].key = key; mrapi_db->sys_sems[i].num_locks = num_locks; } } } release_lock(&mrapi_db->global_lock); if (i == MRAPI_MAX_SEMS) { *id = -1; return MRAPI_FALSE; } else { *id = i; return MRAPI_TRUE; } }
boolean nrnbbs_take_string(const char* key, char* sval) { history("take", key); get_lock(); boolean b = false; FILE* f = fopen(fname(NRNBBS), "r"); if (f != (FILE*)0) { char name[256], val[256]; FILE* f2 = fopen(fname(TMPFILE), "w"); while (fgets(name, 256, f)) { name[strlen(name) -1] = '\0'; if (name[0] == '\0') { continue; } fgets(val, 256, f); val[strlen(val) - 1] = '\0'; if (!b && strcmp(name, key) == 0) { history(" found", val); b = true; strcpy(sval, val); continue; } fprintf(f2, "%s\n%s\n", name, val); } fclose(f2); fclose(f); if (b) { rename(fname(TMPFILE), fname(NRNBBS)); } }else{ b = false; } release_lock(); return b; }
/*---------------------------------------------------------------------------*/ static int hao_initialize(void) { int i = 0; int rc; initialize_lock(&ao_lock); /* serialize */ obtain_lock(&ao_lock); /* initialize variables */ for(i = 0; i < HAO_MAXRULE; i++) { ao_cmd[i] = NULL; ao_tgt[i] = NULL; } /* initialize message buffer */ memset(ao_msgbuf, 0, sizeof(ao_msgbuf)); /* Start message monitoring thread */ rc = create_thread (&haotid, JOINABLE, hao_thread, NULL, "hao_thread"); if(rc) { i = FALSE; WRMSG(HHC00102, "E", strerror(rc)); } else i = TRUE; release_lock(&ao_lock); return(i); }
void thr1(){ while(1){ acquire_lock(); c++; assert(c == 1); c--; release_lock(); } }
/** Close the project (reclaiming all memory) */ bool Project::close ( void ) { if ( ! open() ) return true; if ( ! save() ) return false; Loggable::close(); // write_info(); _is_open = false; *Project::_name = '\0'; *Project::_created_on = '\0'; release_lock( &_lockfd, ".lock" ); delete engine; engine = NULL; return true; }
main() { char *mem_ptr, *ctime(); long now; int n; seg_id = shmget(TIME_MEM_KEY, SEG_SIZE, IPC_CREAT|0777); if( seg_id == -1 ) oops("shmget", 1); mem_ptr = shmat(seg_id, NULL, 0); if (mem_ptr == (void *) -1) oops("shmat", 2); semset_id = semget(TIME_SEM_KEY, 2, (0666|IPC_CREAT|IPC_EXCL)); if (semset_id == -1) oops("segget", 3); set_sem_value(semset_id, 0, 0); set_sem_value(semset_id, 1, 0); signal(SIGINT, cleanup); for (n = 0; n < 60; n++) { time(&now); printf("\t shm_ts2 waiting for lock\n"); wait_and_lock(semset_id); printf("\t shm_ts2 updateing memory\n"); strcpy(mem_ptr, ctime(&now)); sleep(5); release_lock(semset_id); printf("\t shm_ts released lock\n"); sleep(1); } cleanup(0); }
static PyObject * RLock_release(RLock *self, PyObject *args) { long tid = PyThread_get_thread_ident(); if (self->count == 0 || self->owner != tid) { PyErr_SetString(PyExc_RuntimeError, "cannot release un-acquired lock"); return NULL; } if (self->count > 1) { --self->count; Py_RETURN_NONE; } assert(self->count == 1); if (release_lock(&self->sem) != 0) return NULL; self->count = 0; self->owner = 0; Py_RETURN_NONE; }
void nrnbbs_post_string(const char* key, const char* sval) { history("post", key, sval); get_lock(); FILE* f = fopen(fname(NRNBBS), "a"); fprintf(f, "%s\n%s\n", key, sval); FILE* f2 = fopen(fname(NOTIFY), "r"); char name[256]; int i, n, id[10]; n = 0; if (f2) { while( fgets(name, 256, f2) && n < 10) { name[strlen(name) - 1] = '\0'; fscanf(f2, "%d\n", &i); if (strcmp(name, key) == 0) { id[n++] = i; fprintf(f, "nrnbbs_notifying %s\n\n", key); } } fclose(f2); } fclose(f); release_lock(); for (i=0; i < n; ++i) { history(" notify", id[i]); kill(id[i], NOTIFY_SIGNAL); } }
/*-------------------------------------------------------------------*/ int add_socket_devices_to_fd_set (int maxfd, fd_set* readset) { DEVBLK* dev; bind_struct* bs; LIST_ENTRY* pListEntry; obtain_lock(&bind_lock); pListEntry = bind_head.Flink; while (pListEntry != &bind_head) { bs = CONTAINING_RECORD(pListEntry,bind_struct,bind_link); if (bs->sd != -1) /* if listening for connections, */ { dev = bs->dev; FD_SET(bs->sd, readset); /* then add file to set */ if (bs->sd > maxfd) maxfd = bs->sd; } pListEntry = pListEntry->Flink; } release_lock(&bind_lock); return maxfd; }
static void cleanup(debug_context_t * dc) { dc->stop_reason = CTX_STOP_NONE; release_lock(); }
static int lockfile_unlock(lockfile_t *lf) { if (!lf || lf->fd == -1) { return ERROR; } if (release_lock(lf->fd, 0, SEEK_SET, 0) == ERROR) { /* Unlock entire file */ logger.error("Failed to unlock %s: %s", lf->file, strerror(errno)); return ERROR; } close(lf->fd); logger.info("Released PID file lock"); lf->fd = -1; if (remove(lf->file) == ERROR) { logger.warn("Failed to remove %s: %s", lf->file, strerror(errno)); return ERROR; } return OK; }
void timer_thread(void*){ sch_set_priority(2); while(true){ sch_setblock(&events_blockcheck, NULL); sch_setblock(&next_event_blockcheck, NULL); take_lock_exclusive(timer_lock); timer_event *last_event = nullptr; if(next_event){ if(!next_event->cancel){ btos_api::bt_msg_header msg; msg.from = 0; msg.to = next_event->pid; msg.source = extension_id; msg.type = 0; msg.critical = 0; msg.flags = 0; msg.length = sizeof(bt_handle_t); msg.content = malloc(sizeof(bt_handle_t)); timer_info *timer = (*timers)[next_event->timer_id]; *(bt_handle_t*)msg.content = timer->handle_id; timer->active = false; msg_send(msg); } events->erase(events->find(next_event)); last_event = next_event; } update_next_event(); release_lock(timer_lock); if(last_event) delete last_event; } }
static void quit_handler(int signo, siginfo_t *info, void *context) { cl_log(LOG_INFO, "quit_handler called. now releasing lock\n"); release_lock(); cl_log(LOG_INFO, "Shutdown sfex_daemon with EXIT_SUCCESS\n"); exit(EXIT_SUCCESS); }
void TAS_backoff__main(){ while(1){ acquire_lock(); c++; assert(c == 1); c--; release_lock(); } }
memory_entry *memory_entry::allocate(size_t size, void *base, const char *file, int line) { acquire_lock(); // if we're out of free entries, allocate a new chunk if (s_freehead == NULL) { // create a new chunk, and fail if we can't memory_entry *entry = reinterpret_cast<memory_entry *>(osd_malloc_array(memory_block_alloc_chunk * sizeof(memory_entry))); if (entry == NULL) { release_lock(); return NULL; } // add all the entries to the list for (int entrynum = 0; entrynum < memory_block_alloc_chunk; entrynum++) { entry->m_next = s_freehead; s_freehead = entry++; } } // grab a free entry memory_entry *entry = s_freehead; s_freehead = entry->m_next; // populate it entry->m_size = size; entry->m_base = base; entry->m_file = s_tracking ? file : NULL; entry->m_line = s_tracking ? line : 0; entry->m_id = s_curid++; if (LOG_ALLOCS) fprintf(stderr, "#%06d, alloc %d bytes (%s:%d)\n", (UINT32)entry->m_id, static_cast<UINT32>(entry->m_size), entry->m_file, (int)entry->m_line); // add it to the alloc list int hashval = reinterpret_cast<FPTR>(base) % k_hash_prime; entry->m_next = s_hash[hashval]; if (entry->m_next != NULL) entry->m_next->m_prev = entry; entry->m_prev = NULL; s_hash[hashval] = entry; release_lock(); return entry; }
static void hci_smd_recv_event(void) { int len = 0; int rc = 0; struct sk_buff *skb = NULL; struct hci_smd_data *hsmd = &hs; wake_lock(&hs.wake_lock_rx); len = smd_read_avail(hsmd->event_channel); if (len > HCI_MAX_FRAME_SIZE) { BT_ERR("Frame larger than the allowed size, flushing frame"); rc = smd_read(hsmd->event_channel, NULL, len); goto out_event; } while (len > 0) { skb = bt_skb_alloc(len, GFP_ATOMIC); if (!skb) { BT_ERR("Error in allocating socket buffer"); smd_read(hsmd->event_channel, NULL, len); goto out_event; } rc = smd_read(hsmd->event_channel, skb_put(skb, len), len); if (rc < len) { BT_ERR("Error in reading from the event channel"); goto out_event; } skb->dev = (void *)hsmd->hdev; bt_cb(skb)->pkt_type = HCI_EVENT_PKT; skb_orphan(skb); rc = hci_recv_frame(skb); if (rc < 0) { BT_ERR("Error in passing the packet to HCI Layer"); /* * skb is getting freed in hci_recv_frame, making it * to null to avoid multiple access */ skb = NULL; goto out_event; } len = smd_read_avail(hsmd->event_channel); /* * Start the timer to monitor whether the Rx queue is * empty for releasing the Rx wake lock */ BT_DBG("Rx Timer is starting"); mod_timer(&hsmd->rx_q_timer, jiffies + msecs_to_jiffies(RX_Q_MONITOR)); } out_event: release_lock(); if (rc) kfree_skb(skb); }
wxMutexError wxMutex::Unlock() { if (m_locked == 0) return wxMUTEX_UNLOCKED; release_lock(&(p_internal->p_mutex)); m_locked--; return wxMUTEX_NO_ERROR; }
bool events_blockcheck(void*){ if(try_take_lock_exclusive(timer_lock)){ bool ret = (events->size() > 0); release_lock(timer_lock); return ret; } return false; }
/*---------------------------------------------------------------------------*/ static void hao_list(char *arg) { int i; int rc; int size; rc = sscanf(arg, "%d", &i); if(!rc || rc == -1) { /* list all rules */ logmsg(HHCAO004I); size = 0; /* serialize */ obtain_lock(&ao_lock); for(i = 0; i < HAO_MAXRULE; i++) { if(ao_tgt[i]) { logmsg(HHCAO005I, i, ao_tgt[i], (ao_cmd[i] ? ao_cmd[i] : "<not specified>")); size++; } } release_lock(&ao_lock); logmsg(HHCAO006I, size); } else { /* list specific index */ if(i < 0 || i >= HAO_MAXRULE) logmsg(HHCAO009E, HAO_MAXRULE - 1); else { /* serialize */ obtain_lock(&ao_lock); if(!ao_tgt[i]) logmsg(HHCAO008E, i); else logmsg(HHCAO005I, i, ao_tgt[i], (ao_cmd[i] ? ao_cmd[i] : "not specified")); release_lock(&ao_lock); } } }
bool next_event_blockcheck(void*){ if(try_take_lock_exclusive(timer_lock)){ bool ret = next_event ? get_msecs() >= next_event->time : false; release_lock(timer_lock); return ret; } return false; }
hlt_fiber* hlt_fiber_create(hlt_fiber_func func, hlt_execution_context* fctx, void* p, hlt_execution_context* ctx) { assert(ctx); // If there's a fiber available in the local pool, use that. Otherwise // check the global. Otherwise, create one. __hlt_fiber_pool* fiber_pool = ctx->worker ? ctx->worker->fiber_pool : ctx->fiber_pool; assert(fiber_pool); hlt_fiber* fiber = 0; if ( ! fiber_pool->head && hlt_is_multi_threaded() ) { __hlt_fiber_pool* global_pool = __hlt_globals()->synced_fiber_pool; // We do this without locking first, should be fine to encounter a // race. if ( global_pool->size ) { int s = 0; acqire_lock(&s); int n = hlt_config_get()->fiber_max_pool_size / 5; // 20% while ( global_pool->head && n-- ) { fiber = global_pool->head; global_pool->head = fiber->next; --global_pool->size; fiber->next = fiber_pool->head; fiber_pool->head = fiber; ++fiber_pool->size; } // fprintf(stderr, "vid %lu took %lu from global, that now at %lu\n", ctx->vid, hlt_config_get()->fiber_max_pool_size / 10, global_pool->size); release_lock(s); } } if ( fiber_pool->head ) { fiber = fiber_pool->head; fiber_pool->head = fiber_pool->head->next; --fiber_pool->size; fiber->next = 0; assert(fiber->state == IDLE); } else fiber = __hlt_fiber_create(fctx); fiber->run = func; fiber->context = fctx; fiber->cookie = p; return fiber; }
memory_entry *memory_entry::allocate(size_t size, void *base, const char *file, int line) { acquire_lock(); // if we're out of free entries, allocate a new chunk if (s_freehead == NULL) { // create a new chunk, and fail if we can't memory_entry *entry = reinterpret_cast<memory_entry *>(osd_malloc(memory_block_alloc_chunk * sizeof(memory_entry))); if (entry == NULL) { release_lock(); return NULL; } // add all the entries to the list for (int entrynum = 0; entrynum < memory_block_alloc_chunk; entrynum++) { entry->m_next = s_freehead; s_freehead = entry++; } } // grab a free entry memory_entry *entry = s_freehead; s_freehead = entry->m_next; // populate it entry->m_size = size; entry->m_base = base; entry->m_file = file; entry->m_line = line; entry->m_id = s_curid++; // add it to the alloc list int hashval = reinterpret_cast<FPTR>(base) % k_hash_prime; entry->m_next = s_hash[hashval]; if (entry->m_next != NULL) entry->m_next->m_prev = entry; entry->m_prev = NULL; s_hash[hashval] = entry; release_lock(); return entry; }
JobOption BerkeleyQ::peek(const string& queue_name) { if (!this->active) return JobOption(); DbLock lock = acquire_lock(queue_name, DB_LOCK_READ); vector<string> queue = load_queue_vector(queue_name); release_lock(&lock); return !queue.empty() ? find_job(*queue.begin()) : JobOption(); }
void* thr1(void* arg){ acquire_lock(); c++; assert(c == 1); c--; release_lock(); return 0; }
void TicketLock__main(){ PREDICATE(c == 0); PREDICATE(c == 1); while(1){ acquire_lock(); c++;assert(c == 1);c--; release_lock(); } }
void MutexUnlock (mutex_t *m) { abilock_t *lck; if (!m) return; lck = (abilock_t *) m; release_lock (lck); }
/* opened */ DLL_EXPORT int log_open(LOG_WRITER *lw,LOG_CLOSER *lc,void *uw) { int slot; log_route_init(); obtain_lock(&log_route_lock); slot=log_route_search((TID)0); if(slot<0) { release_lock(&log_route_lock); return(-1); } log_routes[slot].t=thread_id(); log_routes[slot].w=lw; log_routes[slot].c=lc; log_routes[slot].u=uw; release_lock(&log_route_lock); return(0); }