void Oed_Dispatcher_timed_exec( T _this_, user_callback *fun, user_callback_arg args, oe_time millis ) { assert(_this_); assert(fun); assert(args); struct timeval tv; struct event *timeout; timeout = Mem_calloc( 1, sizeof *timeout, __FILE__, __LINE__ ); event_base_set( Oed_Dispatcher_get_event_base( _this_ ), timeout ); THOLDER *holder; holder = Mem_calloc( 1, sizeof *holder, __FILE__, __LINE__ ); holder->args = args; holder->fun = fun; evtimer_set( timeout, _timed_exec_cb, holder ); evutil_timerclear(&tv); tv.tv_sec = millis / 1000; tv.tv_usec = (millis % 1000) * 1000; event_add(timeout, &tv); }
static item_ *_create_reg_item(T _this_, oe_scalar *templ_p, oe_time dur, bool consume, user_callback *match_handler, user_callback *timeout_handler, user_callback_arg args) { assert(_this_); //todo: use pool of items and stop alloc'ing item_ *item; item = Mem_calloc(1, sizeof *item, __FILE__, __LINE__); item->dispatcher = _this_; oe_id sid = _gen_id(_this_); item->sid = sid; item->duration = dur; item->consume = consume; item->type = REG; item->object = templ_p; item->match_handler = match_handler; item->timeout_handler = timeout_handler; item->args = args; item->items = NULL; return item; }
static Stream * Stream_new(int pid) { Stream *s = Mem_calloc(1, sizeof(*s)); s->pid = pid; printf("\n[new pid %d]\n", pid); return s; }
int Oed_Dispatcher_stop(T _this_) { assert(_this_); item_ *item; item = Mem_calloc(1, sizeof *item, __FILE__, __LINE__); item->dispatcher = _this_; item->type = TQUIT; oe_id sid = _gen_id(_this_); item->sid = sid; #ifdef OE_USE_THREADS if (_this_->number_of_threads > 0) { _que_push(_this_, item); oe_pthread_join( _this_->thread_id, NULL ); } else { _process_que_item(_this_,item); } #else _process_que_item(_this_,item); #endif //todo: find a way to free mem from pending timer events, or flush them somehow //} return 0; }
static void _watch_for_timeout( item_ *item, struct event_base *ev_base) { if ( item->duration == OEC_FOREVER ) return; T _this_ = item->dispatcher; struct timeval tv; struct event *timeout; timeout = Mem_calloc(1, sizeof *timeout, __FILE__, __LINE__); event_base_set(ev_base, timeout); item->timer_event = timeout; evtimer_set(timeout, _timeout_cb, item); evutil_timerclear(&tv); long sec, msec, dur; dur = item->duration; sec = dur / 1000; if (sec) { msec = dur - (sec * 1000); } else { msec = dur; } tv.tv_sec = sec; tv.tv_usec = msec * 1000; event_add(timeout, &tv); }
Control_msg * Control_msg_new(String *label, Value *value) { Control_msg *msg = Mem_calloc(1, sizeof(*msg)); #error This should dup or something. msg->label = label; msg->value = value; return msg; }
T Oed_Dispatcher_new(size_t number_of_threads, const char *name) { #ifdef OE_NOTHREADS number_of_threads = 0; #endif assert(number_of_threads <= 1); //timer dispatcher can only have 1 thread //for async or 0 for inline T _this_; _this_ = Mem_calloc(1, sizeof *_this_, __FILE__, __LINE__); if (name) { _this_->name = oec_cpy_str(NULL, (oe_scalar) name); } struct event_base *ev_base = event_init(); assert(ev_base); _this_->net_obj = ev_base; QUE *que; que = Mem_calloc(1, sizeof *que, __FILE__, __LINE__); oe_pthread_mutex_init(&que->lock, NULL); oe_pthread_cond_init(&que->cond, NULL); que->running = false; _this_->que = que; _this_->number_of_threads = number_of_threads; _this_->next_unused_id = 101; oe_pthread_mutex_init(&_this_->id_gen_lock, NULL); oe_pthread_cond_init(&_this_->id_gen_cond, NULL); //set up wake up pipe(s) int fds[2]; if (pipe(fds)) { OE_ERR(NULL, "Can't create notify pipe"); assert(0); } _this_->recv_wakeup_fd = fds[0]; _this_->send_wakeup_fd = fds[1]; _this_->items = SKeyTable_new(1000); return _this_; }
void Oed_Dispatcher_unreg (T _this_, oe_id sid) { assert(_this_); item_ *item; item = Mem_calloc(1, sizeof *item, __FILE__, __LINE__); item->dispatcher = _this_; item->type = UNREG; item->sid = sid; item->items = NULL; _schedule_item(_this_, item); }
Audio_buffer * Audio_buffer_new(int rate, int channels, Audio_type t) { Audio_buffer *audio = Mem_calloc(1, sizeof(*audio)); audio->header.atype = t; audio->header.rate = rate; audio->header.channels = channels; audio->header.frame_size = channels * TypeMap[t].size; audio->data = NULL; audio->data_length = 0; return audio; }
static void _schedule_heartbeat( T _this_ ) { item_ *timer_item; timer_item = Mem_calloc( 1, sizeof *timer_item, __FILE__, __LINE__ ); oe_id sid = _gen_id(_this_); timer_item->sid = sid; timer_item->dispatcher = _this_; timer_item->type = HEARTBEAT; struct timeval tv; struct event *timeout; timeout = Mem_calloc(1, sizeof *timeout, __FILE__, __LINE__); event_base_set(_this_->net_obj, timeout); timer_item->timer_event = timeout; evtimer_set(timeout, _heartbeat_cb, timer_item); evutil_timerclear(&tv); tv.tv_sec = 10; event_add(timeout, &tv); }
void Oed_Dispatcher_exec( T _this_, user_callback *fun, user_callback_arg args ) { assert(_this_); item_ *item; item = Mem_calloc(1, sizeof *item, __FILE__, __LINE__); item->type = EXECUTE; item->dispatcher = _this_; item->match_handler = fun; item->args = args; _schedule_item(_this_, item); }
int (Oed_Dispatcher_stats)(T _this_) { assert(_this_); item_ *item; item = Mem_calloc(1, sizeof *item, __FILE__, __LINE__); item->dispatcher = _this_; item->type = DIAG; oe_id sid = _gen_id(_this_); item->sid = sid; _schedule_item(_this_, item); return 0; }
int Oed_Dispatcher_print_diag(T _this_) { assert(_this_); item_ *item; item = Mem_calloc(1, sizeof *item, __FILE__, __LINE__); item->dispatcher = _this_; item->type = DIAG; oe_id sid = _gen_id(_this_); item->sid = sid; _schedule_item(_this_, item); //todo: find a way to free mem from pending timer events, or flush them somehow //} return 0; }
T OEK_default_factory_new(Oed_Dispatcher template_matcher, Oe_Thread_Dispatcher db_dispatcher, OeStore store) { Oek_Impl_ *holder; holder = Mem_calloc( 1, sizeof *holder, __FILE__, __LINE__ ); assert(store); holder->store = store; holder->template_matcher = template_matcher; holder->db_dispatcher = db_dispatcher; Oed_Dispatcher_timed_exec( template_matcher, _lease_reaper_cb, holder, 30000 ); T _this_ = OEK_new(holder, _oek_impl_put, _oek_impl_get, _oek_impl_sta, _oek_impl_upt, _oek_impl_upl, _oekfree, NULL, _oekstats); OeStore_set_fire_write_event_cb( store, _fire_write_event, template_matcher ); OE_DLOG( NULL, OEK_STR_SETUP ); return _this_; }
void Oed_Dispatcher_process(T _this_, DataObject props, bool make_clone) { assert(_this_); OE_TLOG(0, "Oed_Dispatcher_process\n"); //i18n item_ *item; item = Mem_calloc(1, sizeof *item, __FILE__, __LINE__); item->dispatcher = _this_; oe_id sid = _gen_id(_this_); item->sid = sid; item->type = PROCESS; oe_scalar *propsarray = DataObject_toArray(props); if (make_clone) { oe_scalar *clone; _clone_attr_array(propsarray, &clone); item->object = clone; } else { item->object = propsarray; } item->free_alloc = make_clone; //if this is clone, the matcher must free it item->items = NULL; _schedule_item(_this_, item); }
static void *_do_work(void *arg) { T _this_ = (T) arg; assert(_this_); _this_->notify_event = Mem_calloc(1, sizeof *(_this_->notify_event), __FILE__, __LINE__); #ifdef OE_USE_THREADS event_set(_this_->notify_event,_this_->recv_wakeup_fd, EV_READ | EV_PERSIST, _process_que, _this_); #else static void *_dummy(void *arg) {return;} event_set(_this_->notify_event,_this_->recv_wakeup_fd, //event_loop will not run just for EV_READ | EV_PERSIST, _dummy, _this_); //timers, this dummy pipe watching keeps //it alive #endif event_base_set(_this_->net_obj, _this_->notify_event); if (event_add(_this_->notify_event, 0) == -1) { OE_ERR(NULL, "Can't monitor libevent notify pipe"); assert(0); } #ifdef OE_USE_THREADS _schedule_heartbeat(_this_); QUE *que = _this_->que; oe_pthread_mutex_lock(&que->lock); que->running = true; oe_pthread_cond_signal(&que->cond); oe_pthread_mutex_unlock(&que->lock); #endif event_base_loop(_this_->net_obj, 0); return NULL; }
static void ALSAPlayback_tick(Instance *pi) { ALSAio_private *priv = (ALSAio_private *)pi; // int wait_flag = (priv->c.enable ? 0 : 1); int wait_flag; if (!priv->c.enable || !priv->c.rate) { /* Not enabled, or rate not set, wait for an enable message or a Wav buffer. This avoid spinning below. */ wait_flag = 1; } else { /* Enabled and rate set, will generate filler if needed. */ wait_flag = 0; } Handler_message *hm; hm = GetData(pi, wait_flag); // fprintf(stderr, "%s hm=%p\n", __func__, hm); if (hm) { hm->handler(pi, hm->data); ReleaseMessage(&hm,pi); while (pi->pending_messages > 8) { /* This happens when capture clock is faster than playback clock. In cx88play.cmd, it drops a block every ~2s. */ hm = GetData(pi, wait_flag); if (hm->handler == Wav_handler) { Wav_buffer *wav_in = hm->data; double s = (1.0*wav_in->data_length)/ (priv->c.rate * priv->c.channels * priv->c.format_bytes); priv->c.total_dropped += s; fprintf(stderr, "dropping %.4fs of audio (%d %d %d) total %.4f\n", s, priv->c.rate, priv->c.channels, priv->c.format_bytes, priv->c.total_dropped); Wav_buffer_release(&wav_in); } else { /* Should always handle config messages... */ hm->handler(pi, hm->data); } ReleaseMessage(&hm,pi); } } else if (priv->c.enable && priv->c.rate) { /* Filler... */ Wav_buffer *wav_in; int i; //fprintf(stderr, "filler\n"); wav_in = Wav_buffer_new(priv->c.rate, priv->c.channels, priv->c.format_bytes); wav_in->data_length = 16*priv->c.channels*priv->c.format_bytes; wav_in->data = Mem_calloc(1, wav_in->data_length); if (access("/dev/shm/a1", R_OK) == 0) { /* Blip. */ for (i=0; i < wav_in->data_length; i+=2) { //wav_in->data[i] = 0; //wav_in->data[i+1] = (2048 - i); } unlink("/dev/shm/a1"); } Wav_buffer_finalize(wav_in); /* Avoid sending feedback since this was generated locally. */ wav_in->no_feedback = 1; Wav_handler(pi, wav_in); } else { /* Should never get here. */ fprintf(stderr, "WTF?\n"); sleep(1); } }
ArrayU8 * ArrayU8_new(void) { ArrayU8 *a = Mem_calloc(1, sizeof(*a)); return a; }
CSV_table *CSV_load(String *path) { if (!path) { return NULL; } String *contents = File_load_text(path); if (!contents) { return NULL; } CSV_table *csv = Mem_calloc(1, sizeof(*csv)); char *p = contents->bytes; char *newline; char *comma; int columns = 0; int rows = 0; while (1) { //puts("-------"); int ccount = 0; newline = strchr(p, '\n'); if (!newline) { break; } *newline = 0; while (p < newline) { ccount += 1; comma = strchr(p, ','); if (!comma) { comma = newline; /* allow trailing comma or newline to end a row */ } int len = comma - p + 1; /* leave room to null-terminate tmp */ char tmp[len]; strncpy(tmp, p, len); /* does not copy the comma */ tmp[len-1] = 0; /* null-terminate */ //printf("%s\n", tmp); #if 1 IndexedSet_add(csv->_entries, String_new(tmp)); #else { if (csv->_entries.items == 0L) { printf("%d\n", __LINE__); csv->_entries.avail = 2; printf("%d\n", __LINE__); csv->_entries.items = _Mem_calloc(csv->_entries.avail, sizeof(String_new(tmp)), __func__); printf("%d\n", __LINE__); } else if (csv->_entries.avail == csv->_entries.count) { printf("%d\n", __LINE__); csv->_entries.avail *= 2; printf("%d\n", __LINE__); csv->_entries.items = _Mem_realloc(csv->_entries.items, csv->_entries.avail * sizeof(String_new(tmp)), __func__); printf("%d\n", __LINE__); } printf("%d\n", __LINE__); csv->_entries.items[csv->_entries.count] = String_new(tmp); printf("%d\n", __LINE__); csv->_entries.count += 1; printf("%d\n", __LINE__); } #endif p = comma + 1; } if (columns == 0) { columns = ccount; } if (columns != ccount) { fprintf(stderr, "found %d columns, expected %d!\n", columns, ccount); return NULL; } rows += 1; if (*p == 0) { break; } } csv->_rows = rows; csv->_columns = columns; return csv; }
T Oec_IdSet_new (int hint) { T obj; obj = Mem_calloc(1, sizeof *obj, __FILE__, __LINE__); obj->members = Set_new( 100, itemcmp, itemhash ); return obj; }
MotionDetect_result *MotionDetect_result_new(void) { MotionDetect_result *md = Mem_calloc(1, sizeof(*md)); return md; }