void sig_segv (int notUsed) { long uptime2 = 0, p = 0; uptime2 = time (NULL) - uptime; printf ("ERROR! Aborting program. (SIG_SEGV) Uptime: %d hour%s, %d min%s\n", (int) (uptime2 / 3600), (uptime2 / 3600 == 1) ? "" : "s", (int) ((uptime2 / 60) % 60), ((uptime2 / 60) % 60) == 1 ? "" : "s"); Snow ("QUIT :Caught SIG_SEGV! Aborting connection. Uptime: %d hour%s, %d min%s\n", uptime2 / 3600, uptime2 / 3600 == 1 ? "" : "s", (uptime2 / 60) % 60, (uptime2 / 60) % 60 == 1 ? "" : "s"); db_sleep (2); p = getpid (); if (fork () > 0) { db_log ("error.log", "Caught SIGSEGV.. Sent kill -3 and kill -9...\n"); kill (p, 3); kill (p, 9); } db_sleep (1); exit (0); }
void db_close(struct DB *const db) { // Active Dumper thread db->closing = true; db_log(db, "CLOSE: Waiting Active Dumper thread"); pthread_mutex_lock( &(db->mutex_active)); // lock so no active threads is working pthread_cond_broadcast(&(db->cond_active)); pthread_mutex_unlock( &(db->mutex_active)); // lock so no active threads is working pthread_join(db->t_active_dumper, NULL); db_log(db, "CLOSE: Active Dumper thread exited"); // Compaction thread db_log(db, "CLOSE: Waiting for compaction thread"); pthread_mutex_lock(&(db->mutex_current)); pthread_cond_broadcast(&(db->cond_root_consumer)); pthread_cond_broadcast(&(db->cond_root_producer)); pthread_mutex_unlock(&(db->mutex_current)); for (uint64_t n = 0; n < DB_COMPACTION_THREADS_NR; n++) { pthread_join(db->t_compaction[n], NULL); } db_log(db, "CLOSE: Compaction threads exited"); // Meta Dummper thread db_log(db, "CLOSE: Waiting for Meta Dumper thread"); pthread_join(db->t_meta_dumper, NULL); db_log(db, "CLOSE: Meta Dummper thread exited"); // cheap last dump db_dump_meta(db); db_free(db); return; }
/** * Add a channel helper. * 6/22/00 Dan * n now initialized where declared * All pointer arguments now received as pointer to const data. */ void add_helper (const char *chan, const char *uh, long level, size_t num_join, const char *greetz, const char *pass, char mode) { struct helperlist *n = 0; char *ptr = NULL; #if ENCRYPT_PASSWORDS == ON char *salt = "8fei3k"; if ( mode == 0 ) { if ((ptr = crypt (pass, salt)) == NULL) /* encrypt password */ return; } else ptr = (char *)pass; #else ptr = (char *)pass; #endif n = malloc (sizeof (struct helperlist)); if (n == NULL) { db_log ("error.log", "AHHH! No ram left! in add_helper!\n"); return; } memset (n, 0, sizeof (struct helperlist)); NUM_HELPER++; if (chan[0] == '#') { strncpy (n->chan, chan, sizeof (n->chan)); } else { strncpy (n->chan, "#*", sizeof (n->chan)); } strncpy (n->uh, uh, sizeof (n->uh)); strlwr (n->uh); strncpy (n->pass, ptr, sizeof (n->pass)); n->num_join = num_join; n->level = level; strncpy (n->greetz, greetz, min (sizeof (n->greetz) - 1, strlen (greetz))); n->next = helperhead; helperhead = n; }
// takes 0.5s on average // assume table has been detached from db (like memtable => imm) static uint64_t db_table_dump(struct DB *const db, struct Table *const table, uint64_t start_bit) { const double sec0 = debug_time_sec(); const uint64_t mtid = db_aquire_mtid(db); // post process table // must has bloom-filter assert(table->bt); const bool rr = table_retain(table); // logging on failed retaining if (!rr) { // char buffer[4096]; // table_analysis_verbose(table, buffer); db_log(db, "DUMP @%" PRIu64 " [%8" PRIx64 " FAILED!!]\n%s", start_bit / 3, mtid, ""); assert(false); } // analysis and log char buffer[1024]; table_analysis_short(table, buffer); // alloc data area from containermap for items struct ContainerMap *const cm = db->cms[start_bit / 3]; const uint64_t off_main = db_cmap_safe_alloc(db, cm); assert(off_main < cm->total_cap); // dump table data const uint64_t nr_items = table_dump_barrels(table, cm->raw_fd, off_main); // dump meta char metafn[2048]; db_generate_meta_fn(db, mtid, metafn); const bool rdm = table_dump_meta(table, metafn, off_main); assert(rdm); db_log_diff(db, sec0, "DUMP @%" PRIu64 " [%8" PRIx64 " #%08" PRIx64 "] [%08" PRIu64 "] %s", start_bit / 3, mtid, off_main / TABLE_ALIGN, nr_items, buffer); return mtid; }
static void *manymouse_thread(void* data) { fs_log("[MANYMOUSE] Thread running\n"); int k = g_fs_ml_input_device_count; g_first_manymouse_index = k; int mouse_count = ManyMouse_Init(); if (mouse_count < 0) { fs_log("[MANYMOUSE] Initialization failed (%d)\n", mouse_count); } else if (mouse_count == 0) { fs_log("MANYMOUSE: no mice found\n"); // no mice found, so we just quit using the library } for (int i = 0; i < mouse_count; i++) { const char *device = ManyMouse_DeviceName(i); const char *driver = ManyMouse_DriverName(); char *name; if (device[0] == 0 || g_ascii_strcasecmp(device, "mouse") == 0) { name = g_strdup("Mouse: Unnamed Mouse"); } else { name = g_strdup_printf("Mouse: %s", device); } // fs_ml_input_unique_device_name either returns name, or frees it // and return another name, so name must be malloced and owned by // caller name = fs_ml_input_unique_device_name(name); fs_log("MANYMOUSE: Adding %s (%s)\n", name, driver); g_fs_ml_input_devices[k].type = FS_ML_MOUSE; g_fs_ml_input_devices[k].index = k; g_fs_ml_input_devices[k].name = name; g_fs_ml_input_devices[k].alias = g_strdup_printf("MOUSE #%d", i + 2); k += 1; } // when done like this, I believe no memory barrier is needed when the // other thread polls g_manymouse_last_index g_manymouse_last_index = k; if (mouse_count < 0) { // ManyMouse library was not initialized return NULL; } ManyMouseEvent event; fs_ml_event *new_event; while (!fs_ml_is_quitting()) { // printf("..\n"); while (ManyMouse_PollEvent(&event)) { // printf(" -- event type %d -- \n", event.type); if (event.type == MANYMOUSE_EVENT_RELMOTION) { // printf("MANYMOUSE_EVENT_RELMOTION\n"); new_event = fs_ml_alloc_event(); new_event->type = FS_ML_MOUSEMOTION; new_event->motion.device = g_first_manymouse_index + \ event.device; if (event.item == 0) { new_event->motion.xrel = event.value; new_event->motion.yrel = 0; } else if (event.item == 1) { new_event->motion.xrel = 0; new_event->motion.yrel = event.value; } new_event->motion.x = FS_ML_NO_ABSOLUTE_MOUSE_POS; new_event->motion.y = FS_ML_NO_ABSOLUTE_MOUSE_POS; fs_ml_post_event(new_event); // ManyMouseEventType type; // unsigned int device; // unsigned int item; // int value; // int minval; // int maxval; } else if (event.type == MANYMOUSE_EVENT_BUTTON) { db_log(input, "MANYMOUSE: EVENT_BUTTON " "device %d item %d value %d\n", event.device, event.item, event.value); new_event = fs_ml_alloc_event(); new_event->type = event.value ? FS_ML_MOUSEBUTTONDOWN : FS_ML_MOUSEBUTTONUP; new_event->button.state = event.value != 0; new_event->button.device = g_first_manymouse_index + \ event.device; if (event.item == 0) { new_event->button.button = FS_ML_BUTTON_LEFT; } else if (event.item == 1) { new_event->button.button = FS_ML_BUTTON_RIGHT; } else if (event.item == 2) { new_event->button.button = FS_ML_BUTTON_MIDDLE; } else { new_event->button.button = 0; } fs_ml_post_event(new_event); } else if (event.type == MANYMOUSE_EVENT_ABSMOTION) { // printf("MANYMOUSE_EVENT_ABSMOTION\n"); } else if (event.type == MANYMOUSE_EVENT_SCROLL) { db_log(input, "MANYMOUSE: EVENT_SCROLL " "device %d item %d value %d\n", event.device, event.item, event.value); new_event = fs_ml_alloc_event(); new_event->type = FS_ML_MOUSEBUTTONDOWN; new_event->button.state = 1; new_event->button.device = g_first_manymouse_index + \ event.device; new_event->button.button = 0; if (event.item == 0) { if (event.value == 1) { new_event->button.button = FS_ML_BUTTON_WHEELUP; } else { new_event->button.button = FS_ML_BUTTON_WHEELDOWN; } } fs_ml_post_event(new_event); } } fs_ml_usleep(1000); } ManyMouse_Quit(); return NULL; }
uint8_t acp::compress(wstring filename, cmder set, ProgressInfo &pinfo) { uint8_t *ftDATA = new uint8_t[2048000]; uint64_t p_ftDATA = 0; //try open file bitWfile fout; FILE *inf = _wfopen(filename.c_str(), L"rb"); if (inf == NULL) return 0x1; fseek(inf, 0, SEEK_END); pinfo.inlen = ftell(inf); fseek(inf, 0, SEEK_SET); fread(ftDATA, 1, pinfo.inlen, inf); fseek(inf, 0, SEEK_SET); wstring ofilename = filename + L".amc"; if (!fout.open(ofilename)) return 0x2; #if DEBUG //wchar_t db_str[120]; db_log(true); #endif //Head Part FHeader fhead; fhead.diccount = set.dictcount; fhead.bufcount = set.bufcount; fhead.toData(); fout.putChars(20, fhead.data);//write file head uint8_t fnl = (uint8_t)filename.size(); fout.putChars(1, &fnl);//file name length fout.putChars(fnl * 2, (uint8_t*)filename.c_str());//file name data fout.putChars(8, (uint8_t*)&pinfo.inlen);//input file length pinfo.outnow = fout.getpos(); //start Dict_init(set.dictcount); Buffer_init(set.bufcount); uint8_t next_read = 64; ChkItem chkdata; DictOP dOP; BufferOP bOP; CoderOP cOP; DictReport *dRep = new DictReport[set.thread]; BufferReport *bRep = new BufferReport[set.thread]; unique_lock <mutex> lck_DictUse(mtx_Dict_Use); thread T_FD = thread(FindInDict, set.thread, ref(dOP), dRep, ref(chkdata)); dOP.op = 1; T_FD.detach(); //give up lck to enable FindInDict to init cv_Dict_Ready.wait(lck_DictUse, [&] {return dOP.op == 0; }); unique_lock <mutex> lck_BufUse(mtx_Buf_Use); thread T_FB = thread(FindInBuffer, set.thread, ref(bOP), bRep, ref(chkdata)); bOP.op = 1; T_FB.detach(); //give up lck to enable FindInBuffer to init cv_Buf_Ready.wait(lck_BufUse, [&] {return bOP.op == 0; }); /* unique_lock <mutex> lck_CoderUse(mtx_Coder_Use); thread T_C = thread(CoderT, ref(fout), ref(cOP)); cOP.op = 1; T_C.detach(); //give up lck to enable Coder to init cv_Coder_Ready.wait(lck_CoderUse, [&] {return cOP.op == 0; }); */ uint32_t d_cycle = 0; dOP.op = bOP.op = 1; CodeAns Dans, Bans; #if DEBUG auto chkadddata = [&] { for (auto a = 0; a < bOP.len; ++a) { if (ftDATA[p_ftDATA++] != bOP.data[a]) return false; } return true; }; #endif while (Chk_upd(chkdata, inf, next_read) >= 3)//loop untill file end { // swprintf(db_str, L"**Main loop %d .\n",++z_cycle); db_log(db_str); Chk_pre(chkdata, 3); #if DEBUG if (bOP.op == 0xfe) { if (dOP.op == 0xfd) { d_cycle++; //if (d_cycle == 6) //printf("reach\n"); } if (!chkadddata()) { //wprintf(L"\nwrong add data!at %d dict-cycle\n",d_cycle); } } #endif //Dict Part cv_Dict_Use.notify_all(); #if DEBUG_Thr db_log(L"M** noti DC0\n"); #endif cv_Dict_Ready.wait(lck_DictUse, [&] {return dOP.op == 0x7f; }); #if DEBUG_Thr db_log(L"M** wa<- DC0\n"); #endif //pre Dict Ans future<CodeAns> GetDictTestAns = async(Code_TestDict,set.thread,dRep); #if DEBUG_Thr db_log(L"M** make CTD\n"); #endif //prepare data for buffer if (dOP.findlen > 3) Chk_pre(chkdata, dOP.findlen); //Buffer Part cv_Buf_Use.notify_all(); #if DEBUG_Thr db_log(L"M** noti BC0\n"); #endif cv_Buf_Ready.wait(lck_BufUse, [&] {return bOP.op == 0x7f; }); #if DEBUG_Thr db_log(L"M** wa<- BC0\n"); #endif //pre Buf Ans future<CodeAns> GetBufTestAns = async(Code_TestBuffer, set.thread, bRep); #if DEBUG_Thr db_log(L"M** make CTB\n"); #endif //get Ans Dans = GetDictTestAns.get(); #if DEBUG_Thr db_log(L"M** get CTD\n"); #endif bOP.op = 0xfe; Bans = GetBufTestAns.get(); #if DEBUG_Thr db_log(L"M** get CTB\n"); #endif #if DEBUG_Thr db_log(L"**Get Ans\n"); #endif //pre next op //#if DEBUG { swprintf(db_str, L"@Main ANS dans=%d bans=%d\n", Dans.len, Bans.len); db_log(1, db_str, true); } if(Bans.savelen >= Dans.savelen)//Buffer find more&equal byte OR Buffer save more&equal space {// //if (Bans.savelen == (int16_t)0x8000)//both unavaliable if (Bans.savelen < 0)//no saving bits { //put one byte and add buffer cOP.bdata = chkdata.data[0]; bOP.len = next_read = 1; bOP.data[0] = cOP.bdata; cOP.op = 0xfe; dOP.op = 0x33; } else { //put buffer and add dict and buffer cOP.cdata = Bans; dOP.op = 0xfe; bOP.len = dOP.len = next_read = Bans.srclen; memcpy(bOP.data, chkdata.data, next_read); memcpy(dOP.data, chkdata.data, next_read); //bOP.data = dOP.data = Bans.addr; cOP.op = 0xfd; dOP.bOffset = Bans.part_data[5]; dOP.p_b = Bans.p_b; dOP.s_b = Bans.s_b; } } else { //put dict and use dict and add buffer cOP.cdata = Dans; dOP.op = 0xfd; //bOP.data = Dans.addr; dOP.len = bOP.len = next_read = Dans.srclen; memcpy(bOP.data, Dans.addr, next_read); dOP.dID = Dans.dID; cOP.op = 0xfd; } //send data to coder /* cv_Coder_Use.notify_all(); cv_Coder_Ready.wait(lck_CoderUse, [&] {return cOP.op == 0x0; });//give up lock so that coder can do work */ Coder(fout, cOP); //refresh progress-info pinfo.innow += next_read; pinfo.outnow = fout.pos(); } if (chkdata.limit)//still little data { cOP.enddata[0] = chkdata.limit; pinfo.innow += chkdata.limit; cOP.enddata[1] = chkdata.data[0]; cOP.enddata[2] = chkdata.data[1]; } //end bOP.op = cOP.op = dOP.op = 0xff; //end dict cv_Dict_Use.notify_all(); cv_Dict_Ready.wait(lck_DictUse, [&] {return dOP.op == 0x7f; }); //end buffer cv_Buf_Use.notify_all(); cv_Buf_Ready.wait(lck_BufUse, [&] {return bOP.op == 0x7f; }); //end coder /* cv_Coder_Use.notify_all(); cv_Coder_Ready.wait(lck_CoderUse, [&] {return cOP.op == 0x7f; });//give up lock so that coder can do work */ Coder(fout, cOP); Dict_exit(); Buffer_exit(); pinfo.outlen = fout.close(); fclose(inf); #if DEBUG db_log(); #endif return 0x0; }
/* add a track to podcast playlists (if they don't exist they will be created) */ int db_podcast_add_tihm (ipoddb_t *itunesdb, tihm_t *tihm) { tree_node_t *new_pyhm, *new_dohm, *dshm_header, *pyhm_header, *pihm_header, *new_pihm, *temp; db_plhm_t *plhm_data; struct db_pihm *pihm_data; struct db_pyhm *pyhm_data; int i, ret; int next_order = 0; int podcast_group = 0; db_log (itunesdb, 0, "libupod/podcast.c/db_podcast_add_tihm: entering...\n"); /* add track to standard podcast playlist (ds 2) */ if ((ret = db_playlist_get_podcast (itunesdb, 2, NULL)) < 0) if (ret = db_playlist_create_podcast (itunesdb, "Podcasts", 2)) { db_log (itunesdb, ret, "libupod/podcast.c/db_podcast_add_tihm: fatal error. could not create podcast playlist.\n"); return ret; } db_log (itunesdb, 0, "libupod/podcast.c/db_podcast_add_tihm: adding track to playlist %i.%i\n", 2, ret); db_playlist_tihm_add (itunesdb, ret, 2, tihm->num); if ((ret = db_dshm_retrieve (itunesdb, &temp, 3)) < 0) { char *db_name; db_log (itunesdb, 0, "libupod/podcast.c/db_podcast_add_tihm: creating podcast playlists...\n"); db_playlist_get_name (itunesdb, 0, 2, &db_name); /* create master podcast playlist (third data section) */ db_dshm_add (itunesdb, PLHM, 3); db_playlist_create (itunesdb, db_name, 3); free (db_name); db_log (itunesdb, 0, "libupod/podcast.c/db_podcast_add_tihm: complete\n"); } db_playlist_tihm_add (itunesdb, 0, 3, tihm->num); /* update podcast list */ { char *podcast_name; tree_node_t *pyhm_header; if ((ret = db_playlist_get_podcast (itunesdb, 3, &pyhm_header)) < 0) { if (ret = db_playlist_create_podcast (itunesdb, "Podcasts", 3)) { db_log (itunesdb, ret, "libupod/podcast.c/db_podcast_add_tihm: fatal error. could not create podcast playlist.\n"); return ret; } ret = db_playlist_get_podcast (itunesdb, 3, &pyhm_header); } for (i = 0 ; i < tihm->num_dohm ; i++) if (tihm->dohms[i].type == IPOD_ARTIST || tihm->dohms[i].type == IPOD_ALBUM) { podcast_name = tihm->dohms[i].data; break; } db_log (itunesdb, 0, "libupod/podcast.c/db_podcast_add_tihm: adding podcast to group %s\n", podcast_name); pyhm_data = (struct db_pyhm *)pyhm_header->data; if (pyhm_data->num_pihm > 0) { for (i = pyhm_data->num_dohm ; i < pyhm_header->num_children ; i++) { u_int8_t *temp_str; pihm_header = pyhm_header->children[i]; pihm_data = (struct db_pihm *)pihm_header->data; if (pihm_data->pihm != PIHM || !(pihm_data->flag & 0x0000100)) continue; db_dohm_get_string (pihm_header->children[0], &temp_str); if (strcasecmp (temp_str, podcast_name) == 0) { podcast_group = pihm_data->order; db_log (itunesdb, 0, "libupod/podcast.c/db_podcast_add_tihm: found podcast group %i.\n", podcast_group); free (temp_str); break; } free (temp_str); } pihm_data = (struct db_pihm *)pyhm_header->children[pyhm_header->num_children - 1]->data; if (pihm_data->pihm != PIHM) pihm_data = (struct db_pihm *)pyhm_header->children[pyhm_header->num_children - 2]->data; next_order = pihm_data->order + 1; } else next_order = itunesdb->last_entry + 100000; if (!podcast_group) { db_log (itunesdb, 0, "libupod/podcast.c/db_podcast_add_tihm: group doesn't exist yet. creating...\n"); podcast_group = next_order; db_pihm_create_podcast (&new_pihm, podcast_name, next_order++); db_attach (pyhm_header, new_pihm); pyhm_data->num_pihm++; db_log (itunesdb, 0, "libupod/podcast.c/db_podcast_add_tihm: created\n"); } db_pihm_create (&new_pihm, tihm->num, next_order, podcast_group); db_attach (pyhm_header, new_pihm); pyhm_data->num_pihm++; } db_log (itunesdb, 0, "libupod/podcast.c/db_podcast_add_tihm: complete\n"); return 0; }
// pthread static void *thread_active_dumper(void *ptr) { struct DB *const db = (typeof(db))ptr; conc_set_affinity_n(2); while (db->active_table[0]) { // active pthread_mutex_lock(&(db->mutex_active)); if ((db->active_table[0]->volume == 0) && db->closing) { pthread_mutex_unlock(&(db->mutex_active)); table_free(db->active_table[0]); db->active_table[0] = NULL; break; } while ((!table_full(db->active_table[0])) && (!db->closing)) { pthread_cond_wait(&(db->cond_active), &(db->mutex_active)); } // shift active table const uint64_t ticket1 = rwlock_writer_lock(&(db->rwlock)); db->active_table[1] = db->active_table[0]; if (db->closing) { db->active_table[0] = NULL; } else { db->active_table[0] = table_alloc_default(15.0); } rwlock_writer_unlock(&(db->rwlock), ticket1); // notify writers pthread_cond_broadcast(&(db->cond_writer)); pthread_mutex_unlock(&(db->mutex_active)); struct Table *const table1 = db->active_table[1]; if (containermap_unused(db->cms[0]) < 8u) { db_log(db, "ContainerMap is near full, dropping current active-table"); sleep(10); const uint64_t ticket2 = rwlock_writer_lock(&(db->rwlock)); db->active_table[1] = NULL; rwlock_writer_unlock(&(db->rwlock), ticket2); } else if (table1->volume > 0) { const bool rbt = table_build_bloomtable(table1); assert(rbt); const uint64_t mtid = db_table_dump(db, table1, 0); struct MetaTable *const mt = db_load_metatable(db, mtid, db->cms[0]->raw_fd, false); assert(mt); stat_inc_n(&(db->stat.nr_write[0]), TABLE_NR_BARRELS); mt->bt = table1->bt; // mark !active_table[1]->bt before free it // wait for room pthread_mutex_lock(&(db->mutex_current)); while (db->vcroot->cc.count == DB_CONTAINER_NR) { pthread_cond_wait(&(db->cond_root_producer), &(db->mutex_current)); } pthread_mutex_unlock(&(db->mutex_current)); // insert const uint64_t ticket2 = rwlock_writer_lock(&(db->rwlock)); const bool ri = vc_insert_internal(db->vcroot, mt, NULL); assert(ri); stat_inc(&(db->stat.nr_active_dumped)); // alert compaction thread if have work to be done if (db->vcroot->cc.count >= 8) { pthread_mutex_lock(&(db->mutex_current)); pthread_cond_broadcast(&(db->cond_root_consumer)); pthread_mutex_unlock(&(db->mutex_current)); } db->active_table[1] = NULL; rwlock_writer_unlock(&(db->rwlock), ticket2); // post process table1->bt = NULL; } table_free(table1); } pthread_exit(NULL); return NULL; }