/* Callback for our DLL so we can initialize pthread */ BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved ) { #if PTW32_STATIC_LIB switch( fdwReason ) { case DLL_PROCESS_ATTACH: pthread_win32_process_attach_np(); case DLL_THREAD_ATTACH: pthread_win32_thread_attach_np(); break; case DLL_THREAD_DETACH: pthread_win32_thread_detach_np(); break; case DLL_PROCESS_DETACH: pthread_win32_thread_detach_np(); pthread_win32_process_detach_np(); break; } #endif return TRUE; }
/* * Dear c++: Please don't mangle this name. -thanks */ extern "C" #endif /* __cplusplus */ BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved) { BOOL result = PTW32_TRUE; switch (fdwReason) { case DLL_PROCESS_ATTACH: result = pthread_win32_process_attach_np (); break; case DLL_THREAD_ATTACH: /* * A thread is being created */ result = pthread_win32_thread_attach_np (); break; case DLL_THREAD_DETACH: /* * A thread is exiting cleanly */ result = pthread_win32_thread_detach_np (); break; case DLL_PROCESS_DETACH: (void) pthread_win32_thread_detach_np (); result = pthread_win32_process_detach_np (); break; } return (result); } /* DllMain */
BOOL APIENTRY DllMain(HMODULE hm, DWORD why, LPVOID rsrv) { WSADATA wsa; switch (why) { case DLL_PROCESS_ATTACH: if(WSAStartup(MAKEWORD(2,2), &wsa)) return FALSE; fix_paths(); return pthread_win32_process_attach_np(); break; case DLL_THREAD_ATTACH: return pthread_win32_thread_attach_np (); break; case DLL_THREAD_DETACH: return pthread_win32_thread_detach_np (); break; case DLL_PROCESS_DETACH: WSACleanup(); pthread_win32_thread_detach_np (); return pthread_win32_process_detach_np (); break; } }
int main( int _argc, char * * _argv ) { if( _argc < 3 ) { fprintf( stderr, "not enough arguments\n" ); return -1; } #ifdef LMMS_BUILD_WIN32 // (non-portable) initialization of statically linked pthread library pthread_win32_process_attach_np(); pthread_win32_thread_attach_np(); #endif RemoteZynAddSubFx * remoteZASF = new RemoteZynAddSubFx( atoi( _argv[1] ), atoi( _argv[2] ) ); remoteZASF->run(); delete remoteZASF; #ifdef LMMS_BUILD_WIN32 pthread_win32_thread_detach_np(); pthread_win32_process_detach_np(); #endif return 0; }
int main(int argc, char **argv) { #ifdef MINGW pthread_win32_thread_attach_np(); #endif Fl::lock(); MainWindow mainWindow; mainWindow.show(argc, argv); ToxWorker *toxWorker; try { toxWorker = new ToxWorker(&mainWindow); } catch (ToxWorkerException &e) { Dialog::notify("Can't start Tox or ToxTun"); return EXIT_FAILURE; } mainWindow.setToxWorker(toxWorker); Fl::run(); mainWindow.setToxWorker(nullptr); delete toxWorker; #ifdef MINGW pthread_win32_thread_detach_np(); #endif return EXIT_SUCCESS; }
/**************************************************************************** * main: ****************************************************************************/ int main( int argc, char **argv ) { x264_param_t param; cli_opt_t opt; int ret; #ifdef PTW32_STATIC_LIB pthread_win32_process_attach_np(); pthread_win32_thread_attach_np(); #endif #ifdef _WIN32 _setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stdout), _O_BINARY); #endif x264_param_default( ¶m ); /* Parse command line */ if( Parse( argc, argv, ¶m, &opt ) < 0 ) return -1; /* Control-C handler */ signal( SIGINT, SigIntHandler ); ret = Encode( ¶m, &opt ); #ifdef PTW32_STATIC_LIB pthread_win32_thread_detach_np(); pthread_win32_process_detach_np(); #endif return ret; }
void *Thread::ThreadRun(void *myThread) { Thread *thread = reinterpret_cast<Thread*>(myThread); thread->mRunning = true; #ifdef PTW32_STATIC_LIB pthread_win32_thread_attach_np(); #endif try { thread->mTask->run(); } catch(const std::exception &e) { LogWarn("Thread::ThreadRun", String("Unhandled exception in thread: ") + e.what()); } catch(...) { LogWarn("Thread::ThreadRun", String("Unhandled unknown exception in thread")); } thread->mRunning = false; if(thread->mAutoDelete) delete thread; #ifdef PTW32_STATIC_LIB pthread_win32_thread_detach_np(); #endif pthread_exit(NULL); return NULL; }
/***************************************************************************** * CloseEncoder: x264 encoder destruction *****************************************************************************/ static void Close( vlc_object_t *p_this ) { encoder_t *p_enc = (encoder_t *)p_this; encoder_sys_t *p_sys = p_enc->p_sys; free( p_sys->psz_stat_name ); if( p_sys->h ) x264_encoder_close( p_sys->h ); #ifdef PTW32_STATIC_LIB vlc_value_t lock, count; var_Get( p_enc->p_libvlc, "pthread_win32_mutex", &lock ); vlc_mutex_lock( lock.p_address ); var_Get( p_enc->p_libvlc, "pthread_win32_count", &count ); count.i_int--; var_Set( p_enc->p_libvlc, "pthread_win32_count", count ); if( count.i_int == 0 ) { pthread_win32_thread_detach_np(); pthread_win32_process_detach_np(); msg_Dbg( p_enc, "pthread-win32 deinitialized" ); } vlc_mutex_unlock( lock.p_address ); #endif free( p_sys->p_buffer ); free( p_sys ); }
void *Thread::ThreadCall(void *myWrapper) { Wrapper *wrapper = reinterpret_cast<Wrapper*>(myWrapper); wrapper->thread->mRunning = true; #ifdef PTW32_STATIC_LIB pthread_win32_thread_attach_np(); #endif try { wrapper->call(); } catch(const std::exception &e) { LogWarn("Thread::ThreadCall", String("Unhandled exception in thread: ") + e.what()); } catch(...) { LogWarn("Thread::ThreadCall", String("Unhandled unknown exception in thread")); } delete wrapper; wrapper->thread->mRunning = false; #ifdef PTW32_STATIC_LIB pthread_win32_thread_detach_np(); #endif pthread_exit(NULL); return NULL; }
BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved) { switch (reason) { case DLL_PROCESS_ATTACH: timeBeginPeriod(1); #ifdef PTW32_STATIC_LIB pthread_win32_process_attach_np(); #endif break; case DLL_PROCESS_DETACH: timeEndPeriod(1); #ifdef PTW32_STATIC_LIB pthread_win32_process_detach_np(); #endif break; case DLL_THREAD_ATTACH: #ifdef PTW32_STATIC_LIB pthread_win32_thread_attach_np(); #endif break; case DLL_THREAD_DETACH: #ifdef PTW32_STATIC_LIB pthread_win32_thread_detach_np(); #endif break; } UNUSED_PARAMETER(hinst_dll); UNUSED_PARAMETER(reserved); return true; }
static void x264_threading_destroy( void ) { #if PTW32_STATIC_LIB pthread_win32_thread_detach_np(); pthread_win32_process_detach_np(); #else x264_win32_threading_destroy(); #endif }
void* Thread::_internal_callback( void* itsme ) { jASSUME( itsme != NULL ); pthread_win32_thread_attach_np(); Thread& owner = *((Thread*)itsme); owner.m_returncode = owner.Callback(); owner.m_terminated = true; pthread_win32_thread_detach_np(); return NULL; }
threadpool::~threadpool() { /* We previously sent the termination message to all of the sub-threads here. * However, their terminating caused wacky problems with the malloc library. * So we just leave them floating around now. Doesn't matter much, because * the main process will die soon enough. */ kill_all_workers(); /* Release our resources */ pthread_cond_destroy(&TOMAIN); pthread_cond_destroy(&TOWORKER); #ifdef WIN32 pthread_win32_process_detach_np(); pthread_win32_thread_detach_np(); #endif }
//FILE* ff ; int main_bak( int argc, char **argv ) { x264_param_t param; cli_opt_t opt; int ret; //ff = fopen("ff_org.264", "wb"); initDebugLog("debug_org.log"); DEBUG_LOG(INF, "main begin"); #ifdef PTW32_STATIC_LIB pthread_win32_process_attach_np(); pthread_win32_thread_attach_np(); #endif #ifdef _WIN32 _setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stdout), _O_BINARY); #endif /* 初始化输入参数结构体,设置为默认值 */ x264_param_default( ¶m ); /* 解析命令行,设置param(包括函数指针),并保存到opt的文件中。打开输出的文件.Parse command line */ if( Parse( argc, argv, ¶m, &opt ) < 0 ) return -1; /* 是否响应Ctrl+C终端。Control-C handler */ signal( SIGINT, SigIntHandler ); /* 编码主控函数,编码输入输出文件分别是opt中的hin和hout */ ret = Encode( ¶m, &opt ); #ifdef PTW32_STATIC_LIB pthread_win32_thread_detach_np(); pthread_win32_process_detach_np(); #endif DEBUG_LOG(INF, "main end"); return ret; }
BOOL WINAPI DllMain(HINSTANCE hinst_dll, DWORD reason, LPVOID reserved) { switch (reason) { case DLL_PROCESS_ATTACH: pthread_win32_process_attach_np(); break; case DLL_PROCESS_DETACH: pthread_win32_process_detach_np(); break; case DLL_THREAD_ATTACH: pthread_win32_thread_attach_np(); break; case DLL_THREAD_DETACH: pthread_win32_thread_detach_np(); break; } return true; }
threadpool::~threadpool() { /* We previously sent the termination message to all of the sub-threads here. * However, their terminating caused wacky problems with the malloc library. * So we just leave them floating around now. Doesn't matter much, because * the main process will die soon enough. */ #if 0 for(int i=0;i<num_threads;i++){ this->schedule_work(0); } #endif /* Release our resources */ fs.close_all(); pthread_mutex_destroy(&M); pthread_cond_destroy(&TOMAIN); pthread_cond_destroy(&TOWORKER); #ifdef WIN32 pthread_win32_process_detach_np(); pthread_win32_thread_detach_np(); #endif }
/* * ptw32_throw * * All canceled and explicitly exited POSIX threads go through * here. This routine knows how to exit both POSIX initiated threads and * 'implicit' POSIX threads for each of the possible language modes (C, * C++, and SEH). */ void ptw32_throw (DWORD exception) { /* * Don't use pthread_self() to avoid creating an implicit POSIX thread handle * unnecessarily. */ pthread_t self = (pthread_t) pthread_getspecific (ptw32_selfThreadKey); #ifdef __CLEANUP_SEH DWORD exceptionInformation[3]; #endif if (exception != PTW32_EPS_CANCEL && exception != PTW32_EPS_EXIT) { /* Should never enter here */ exit (1); } if (NULL == self || self->implicit) { /* * We're inside a non-POSIX initialised Win32 thread * so there is no point to jump or throw back to. Just do an * explicit thread exit here after cleaning up POSIX * residue (i.e. cleanup handlers, POSIX thread handle etc). */ unsigned exitCode = 0; switch (exception) { case PTW32_EPS_CANCEL: exitCode = (unsigned) PTHREAD_CANCELED; break; case PTW32_EPS_EXIT: exitCode = (unsigned) self->exitStatus;; break; } pthread_win32_thread_detach_np (); #if ! defined (__MINGW32__) || defined (__MSVCRT__) _endthreadex (exitCode); #else _endthread (); #endif } #ifdef __CLEANUP_SEH exceptionInformation[0] = (DWORD) (exception); exceptionInformation[1] = (DWORD) (0); exceptionInformation[2] = (DWORD) (0); RaiseException (EXCEPTION_PTW32_SERVICES, 0, 3, exceptionInformation); #else /* __CLEANUP_SEH */ #ifdef __CLEANUP_C ptw32_pop_cleanup_all (1); longjmp (self->start_mark, exception); #else /* __CLEANUP_C */ #ifdef __CLEANUP_CXX switch (exception) { case PTW32_EPS_CANCEL: throw ptw32_exception_cancel (); break; case PTW32_EPS_EXIT: throw ptw32_exception_exit (); break; } #else #error ERROR [__FILE__, line __LINE__]: Cleanup type undefined. #endif /* __CLEANUP_CXX */ #endif /* __CLEANUP_C */ #endif /* __CLEANUP_SEH */ /* Never reached */ }
static void on_process_exit(void) { pthread_win32_thread_detach_np (); pthread_win32_process_detach_np (); }
static int on_process_exit(void) { pthread_win32_thread_detach_np (); pthread_win32_process_detach_np (); return 0; }
/**************************************************************************** * Function: WorkerThread * * Description: * Implements a thread pool worker. * Worker waits for a job to become available. * Worker picks up persistent jobs first, high priority, med priority, * then low priority. * If worker remains idle for more than specified max, the worker * is released. * Internal Only. * Parameters: * void * arg -> is cast to ThreadPool * *****************************************************************************/ static void *WorkerThread( void *arg ) { time_t start = 0; ThreadPoolJob *job = NULL; ListNode *head = NULL; struct timespec timeout; int retCode = 0; int persistent = -1; ThreadPool *tp = ( ThreadPool *) arg; // allow static linking #ifdef WIN32 #ifdef PTW32_STATIC_LIB pthread_win32_thread_attach_np(); #endif #endif assert( tp != NULL ); // Increment total thread count ithread_mutex_lock( &tp->mutex ); tp->totalThreads++; ithread_cond_broadcast( &tp->start_and_shutdown ); ithread_mutex_unlock( &tp->mutex ); SetSeed(); StatsTime( &start ); while( 1 ) { ithread_mutex_lock( &tp->mutex ); if( job ) { FreeThreadPoolJob( tp, job ); job = NULL; } retCode = 0; tp->stats.idleThreads++; tp->stats.totalWorkTime += ( StatsTime( NULL ) - start ); // work time StatsTime( &start ); // idle time if( persistent == 1 ) { // Persistent thread // becomes a regular thread tp->persistentThreads--; } if( persistent == 0 ) { tp->stats.workerThreads--; } // Check for a job or shutdown while( tp->lowJobQ.size == 0 && tp->medJobQ.size == 0 && tp->highJobQ.size == 0 && !tp->persistentJob && !tp->shutdown ) { // If wait timed out // and we currently have more than the // min threads, or if we have more than the max threads // (only possible if the attributes have been reset) // let this thread die. if( ( retCode == ETIMEDOUT && tp->totalThreads > tp->attr.minThreads ) || ( tp->attr.maxThreads != -1 && tp->totalThreads > tp->attr.maxThreads ) ) { tp->stats.idleThreads--; tp->totalThreads--; ithread_cond_broadcast( &tp->start_and_shutdown ); ithread_mutex_unlock( &tp->mutex ); #ifdef WIN32 #ifdef PTW32_STATIC_LIB // allow static linking pthread_win32_thread_detach_np (); #endif #endif return NULL; } SetRelTimeout( &timeout, tp->attr.maxIdleTime ); // wait for a job up to the specified max time retCode = ithread_cond_timedwait( &tp->condition, &tp->mutex, &timeout ); } tp->stats.idleThreads--; tp->stats.totalIdleTime += ( StatsTime( NULL ) - start ); // idle time StatsTime( &start ); // work time // bump priority of starved jobs BumpPriority( tp ); // if shutdown then stop if( tp->shutdown ) { tp->totalThreads--; ithread_cond_broadcast( &tp->start_and_shutdown ); ithread_mutex_unlock( &tp->mutex ); #ifdef WIN32 #ifdef PTW32_STATIC_LIB // allow static linking pthread_win32_thread_detach_np (); #endif #endif return NULL; } else { // Pick up persistent job if available if( tp->persistentJob ) { job = tp->persistentJob; tp->persistentJob = NULL; tp->persistentThreads++; persistent = 1; ithread_cond_broadcast( &tp->start_and_shutdown ); } else { tp->stats.workerThreads++; persistent = 0; // Pick the highest priority job if( tp->highJobQ.size > 0 ) { head = ListHead( &tp->highJobQ ); job = ( ThreadPoolJob *) head->item; CalcWaitTime( tp, HIGH_PRIORITY, job ); ListDelNode( &tp->highJobQ, head, 0 ); } else if( tp->medJobQ.size > 0 ) { head = ListHead( &tp->medJobQ ); job = ( ThreadPoolJob *) head->item; CalcWaitTime( tp, MED_PRIORITY, job ); ListDelNode( &tp->medJobQ, head, 0 ); } else if( tp->lowJobQ.size > 0 ) { head = ListHead( &tp->lowJobQ ); job = ( ThreadPoolJob *) head->item; CalcWaitTime( tp, LOW_PRIORITY, job ); ListDelNode( &tp->lowJobQ, head, 0 ); } else { // Should never get here assert( 0 ); tp->stats.workerThreads--; tp->totalThreads--; ithread_cond_broadcast( &tp->start_and_shutdown ); ithread_mutex_unlock( &tp->mutex ); return NULL; } } } ithread_mutex_unlock( &tp->mutex ); if( SetPriority( job->priority ) != 0 ) { // In the future can log // info } else { // In the future can log // info } // run the job job->func( job->arg ); // return to Normal SetPriority( DEFAULT_PRIORITY ); } }
int main(int argc, char* argv[]) { char *albumdir = 0, *musicfilename, *file_path = 0; int i, area_idx; sacd_reader_t *sacd_reader; #ifdef PTW32_STATIC_LIB pthread_win32_process_attach_np(); pthread_win32_thread_attach_np(); #endif init(); if (parse_options(argc, argv)) { setlocale(LC_ALL, ""); if (fwide(stdout, 1) < 0) { fprintf(stderr, "ERROR: Output not set to wide.\n"); } // default to 2 channel if (opts.two_channel == 0 && opts.multi_channel == 0) { opts.two_channel = 1; } sacd_reader = sacd_open(opts.input_device); if (sacd_reader) { handle = scarletbook_open(sacd_reader, 0); if (handle) { if (opts.print) { scarletbook_print(handle); } if (opts.output_dsf || opts.output_iso || opts.output_dsdiff || opts.output_dsdiff_em || opts.export_cue_sheet) { output = scarletbook_output_create(handle, handle_status_update_track_callback, handle_status_update_progress_callback, safe_fwprintf); // select the channel area area_idx = ((has_multi_channel(handle) && opts.multi_channel) || !has_two_channel(handle)) ? handle->mulch_area_idx : handle->twoch_area_idx; albumdir = (strlen(opts.output_file) > 0 ? strdup(opts.output_file) : get_album_dir(handle)); if (opts.output_iso) { uint32_t total_sectors = sacd_get_total_sectors(sacd_reader); #ifdef SECTOR_LIMIT #define FAT32_SECTOR_LIMIT 2090000 uint32_t sector_size = FAT32_SECTOR_LIMIT; uint32_t sector_offset = 0; if (total_sectors > FAT32_SECTOR_LIMIT) { musicfilename = (char *) malloc(512); file_path = make_filename(0, 0, albumdir, "iso"); for (i = 1; total_sectors != 0; i++) { sector_size = min(total_sectors, FAT32_SECTOR_LIMIT); snprintf(musicfilename, 512, "%s.%03d", file_path, i); scarletbook_output_enqueue_raw_sectors(output, sector_offset, sector_size, musicfilename, "iso"); sector_offset += sector_size; total_sectors -= sector_size; } free(musicfilename); } else #endif { get_unique_filename(&albumdir, "iso"); file_path = make_filename(0, 0, albumdir, "iso"); scarletbook_output_enqueue_raw_sectors(output, 0, total_sectors, file_path, "iso"); } } else if (opts.output_dsdiff_em) { get_unique_filename(&albumdir, "dff"); file_path = make_filename(0, 0, albumdir, "dff"); scarletbook_output_enqueue_track(output, area_idx, 0, file_path, "dsdiff_edit_master", (opts.convert_dst ? 1 : handle->area[area_idx].area_toc->frame_format != FRAME_FORMAT_DST)); } else if (opts.output_dsf || opts.output_dsdiff) { // create the output folder get_unique_dir(0, &albumdir); recursive_mkdir(albumdir, 0774); // fill the queue with items to rip for (i = 0; i < handle->area[area_idx].area_toc->track_count; i++) { if (opts.select_tracks && opts.selected_tracks[i] == 0) continue; musicfilename = get_music_filename(handle, area_idx, i, opts.output_file); if (opts.output_dsf) { file_path = make_filename(0, albumdir, musicfilename, "dsf"); scarletbook_output_enqueue_track(output, area_idx, i, file_path, "dsf", 1 /* always decode to DSD */); } else if (opts.output_dsdiff) { file_path = make_filename(0, albumdir, musicfilename, "dff"); scarletbook_output_enqueue_track(output, area_idx, i, file_path, "dsdiff", (opts.convert_dst ? 1 : handle->area[area_idx].area_toc->frame_format != FRAME_FORMAT_DST)); } free(musicfilename); free(file_path); file_path = 0; } } if (opts.export_cue_sheet) { char *cue_file_path = make_filename(0, 0, albumdir, "cue"); #ifdef _WIN32 wchar_t *wide_filename = (wchar_t *) charset_convert(cue_file_path, strlen(cue_file_path), "UTF-8", sizeof(wchar_t) == 2 ? "UCS-2-INTERNAL" : "UCS-4-INTERNAL"); #else wchar_t *wide_filename = (wchar_t *) charset_convert(cue_file_path, strlen(cue_file_path), "UTF-8", "WCHAR_T"); #endif fwprintf(stdout, L"Exporting CUE sheet [%ls]\n", wide_filename); if (!file_path) file_path = make_filename(0, 0, albumdir, "dff"); write_cue_sheet(handle, file_path, area_idx, cue_file_path); free(cue_file_path); free(wide_filename); } free(file_path); started_processing = time(0); scarletbook_output_start(output); scarletbook_output_destroy(output); fprintf(stdout, "\rWe are done.. \n"); } scarletbook_close(handle); free(albumdir); } } sacd_close(sacd_reader); #ifndef _WIN32 freopen(0, "w", stdout); #endif if (fwide(stdout, -1) >= 0) { fprintf(stderr, "ERROR: Output not set to byte oriented.\n"); } } free_lock(g_fwprintf_lock); destroy_logging(); #ifdef PTW32_STATIC_LIB pthread_win32_process_detach_np(); pthread_win32_thread_detach_np(); #endif printf("\n"); return 0; }
static void detach_ptw32(void) { pthread_win32_thread_detach_np(); pthread_win32_process_detach_np(); }