<commit_before>/*++
Copyright (c) 2011 Microsoft Corporation

Module Name:

    scoped_timer.cpp

Abstract:

    <abstract>

Author:

    <NAME> (leonardo) 2011-04-26.

Revision History:

--*/
#ifdef _CYGWIN
// Hack to make CreateTimerQueueTimer available on cygwin
#define _WIN32_WINNT 0x0600
#endif

#include"z3_exception.h"
#include"z3_omp.h"
#if defined(_WINDOWS) || defined(_CYGWIN)
// Windows
#include<windows.h>
#elif defined(__APPLE__) && defined(__MACH__)
// Mac OS X
#include<mach/mach.h>
#include<mach/clock.h>
#include<sys/time.h>
#include<sys/errno.h>
#include<pthread.h>
#elif defined(_LINUX_) || defined(_FREEBSD_)
// Linux
#include<csignal>
#include<ctime>
#include<memory.h>
#include"warning.h"
   #ifdef _LINUX_
   #define CLOCKID CLOCK_PROCESS_CPUTIME_ID
   #else
   // FreeBSD does not support CLOCK_PROCESS_CPUTIME_ID 
   #define CLOCKID CLOCK_PROF
   #endif
#define SIG     SIGRTMIN
// ---------
#else
// Other platforms
#endif 

#include"scoped_timer.h"
#ifdef _CYGWIN
#undef min
#undef max
#endif
#include"util.h"
#include<limits.h>

struct scoped_timer::imp {
    event_handler *  m_eh;
#if defined(_WINDOWS) || defined(_CYGWIN)
    HANDLE           m_timer;
    bool             m_first;
#elif defined(__APPLE__) && defined(__MACH__)
    // Mac OS X
    pthread_t        m_thread_id;
    pthread_attr_t   m_attributes;
    unsigned         m_interval;    
    pthread_cond_t   m_condition_var;
#elif defined(_LINUX_) || defined(_FREEBSD_)
    // Linux & FreeBSD
    static void *   g_timer;
    void            (*m_old_handler)(int);
    void *          m_old_timer;
    timer_t         m_timerid;
#else
    // Other
#endif

#if defined(_WINDOWS) || defined(_CYGWIN)
    static void CALLBACK abort_proc(PVOID param, BOOLEAN timer_or_wait_fired) {
        imp * obj = static_cast<imp*>(param);
        if (obj->m_first) {
            obj->m_first = false;
        }
        else {
            obj->m_eh->operator()();
        }
    }
#elif defined(__APPLE__) && defined(__MACH__)
    // Mac OS X
    static void * thread_func(void * arg) {
        scoped_timer::imp * st = static_cast<scoped_timer::imp*>(arg);  

        pthread_mutex_t  mutex;
        clock_serv_t host_clock;
        struct timespec abstime;
        mach_timespec_t now;
        unsigned long long nano = static_cast<unsigned long long>(st->m_interval) * 1000000ull;

        host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &host_clock);

        if (pthread_mutex_init(&mutex, NULL) != 0)
            throw default_exception("failed to initialize timer mutex");
        if (pthread_cond_init(&st->m_condition_var, NULL) != 0)
            throw default_exception("failed to initialize timer condition variable");

        abstime.tv_sec  = nano / 1000000000ull;
        abstime.tv_nsec = nano % 1000000000ull;

        pthread_mutex_lock(&mutex);
        clock_get_time(host_clock, &now);
        ADD_MACH_TIMESPEC(&abstime, &now);
        int e = pthread_cond_timedwait(&st->m_condition_var, &mutex, &abstime);
        if (e != 0 && e != ETIMEDOUT)
            throw default_exception("failed to start timed wait");
        st->m_eh->operator()();
        pthread_mutex_unlock(&mutex);

        if (pthread_mutex_destroy(&mutex) != 0)
            throw default_exception("failed to destroy pthread mutex");
        if (pthread_cond_destroy(&st->m_condition_var) != 0)
            throw default_exception("failed to destroy pthread condition variable");
        return st;
    }
#elif defined(_LINUX_)
    static void sig_handler(int) {
       static_cast<imp*>(g_timer)->m_eh->operator()();
    }
#else
    // Other
#endif


    imp(unsigned ms, event_handler * eh):
        m_eh(eh) {
#if defined(_WINDOWS) || defined(_CYGWIN)
        m_first = true;
        CreateTimerQueueTimer(&m_timer,			
                              NULL,				
                              abort_proc,
                              this,
                              0,				
                              ms,				
                              WT_EXECUTEINTIMERTHREAD);	
#elif defined(__APPLE__) && defined(__MACH__)
        // Mac OS X<commit_msg>fixed FreeBSD support<commit_after>/*++
Copyright (c) 2011 Microsoft Corporation

Module Name:

    scoped_timer.cpp

Abstract:

    <abstract>

Author:

    <NAME> (leonardo) 2011-04-26.

Revision History:

--*/
#ifdef _CYGWIN
// Hack to make CreateTimerQueueTimer available on cygwin
#define _WIN32_WINNT 0x0600
#endif

#include"z3_exception.h"
#include"z3_omp.h"
#if defined(_WINDOWS) || defined(_CYGWIN)
// Windows
#include<windows.h>
#elif defined(__APPLE__) && defined(__MACH__)
// Mac OS X
#include<mach/mach.h>
#include<mach/clock.h>
#include<sys/time.h>
#include<sys/errno.h>
#include<pthread.h>
#elif defined(_LINUX_) || defined(_FREEBSD_)
// Linux
#include<csignal>
#include<ctime>
#include<memory.h>
#include"warning.h"
   #ifdef _LINUX_
   #define CLOCKID CLOCK_PROCESS_CPUTIME_ID
   #else
   // FreeBSD does not support CLOCK_PROCESS_CPUTIME_ID 
   #define CLOCKID CLOCK_PROF
   #endif
#define SIG     SIGRTMIN
// ---------
#else
// Other platforms
#endif 

#include"scoped_timer.h"
#ifdef _CYGWIN
#undef min
#undef max
#endif
#include"util.h"
#include<limits.h>

struct scoped_timer::imp {
    event_handler *  m_eh;
#if defined(_WINDOWS) || defined(_CYGWIN)
    HANDLE           m_timer;
    bool             m_first;
#elif defined(__APPLE__) && defined(__MACH__)
    // Mac OS X
    pthread_t        m_thread_id;
    pthread_attr_t   m_attributes;
    unsigned         m_interval;    
    pthread_cond_t   m_condition_var;
#elif defined(_LINUX_) || defined(_FREEBSD_)
    // Linux & FreeBSD
    static void *   g_timer;
    void            (*m_old_handler)(int);
    void *          m_old_timer;
    timer_t         m_timerid;
#else
    // Other
#endif

#if defined(_WINDOWS) || defined(_CYGWIN)
    static void CALLBACK abort_proc(PVOID param, BOOLEAN timer_or_wait_fired) {
        imp * obj = static_cast<imp*>(param);
        if (obj->m_first) {
            obj->m_first = false;
        }
        else {
            obj->m_eh->operator()();
        }
    }
#elif defined(__APPLE__) && defined(__MACH__)
    // Mac OS X
    static void * thread_func(void * arg) {
        scoped_timer::imp * st = static_cast<scoped_timer::imp*>(arg);  

        pthread_mutex_t  mutex;
        clock_serv_t host_clock;
        struct timespec abstime;
        mach_timespec_t now;
        unsigned long long nano = static_cast<unsigned long long>(st->m_interval) * 1000000ull;

        host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &host_clock);

        if (pthread_mutex_init(&mutex, NULL) != 0)
            throw default_exception("failed to initialize timer mutex");
        if (pthread_cond_init(&st->m_condition_var, NULL) != 0)
            throw default_exception("failed to initialize timer condition variable");

        abstime.tv_sec  = nano / 1000000000ull;
        abstime.tv_nsec = nano % 1000000000ull;

        pthread_mutex_lock(&mutex);
        clock_get_time(host_clock, &now);
        ADD_MACH_TIMESPEC(&abstime, &now);
        int e = pthread_cond_timedwait(&st->m_condition_var, &mutex, &abstime);
        if (e != 0 && e != ETIMEDOUT)
            throw default_exception("failed to start timed wait");
        st->m_eh->operator()();
        pthread_mutex_unlock(&mutex);

        if (pthread_mutex_destroy(&mutex) != 0)
            throw default_exception("failed to destroy pthread mutex");
        if (pthread_cond_destroy(&st->m_condition_var) != 0)
            throw default_exception("failed to destroy pthread condition variable");
        return st;
    }
#elif defined(_LINUX_) || defined(_FREEBSD_)
    static void sig_handler(int) {
       static_cast<imp*>(g_timer)->m_eh->operator()();
    }
#else
    // Other
#endif


    imp(unsigned ms, event_handler * eh):
        m_eh(eh) {
#if defined(_WINDOWS) || defined(_CYGWIN)
        m_first = true;
        CreateTimerQueueTimer(&m_timer,			
                              NULL,				
                              abort_proc,
                              this,
                              0,				
                              ms,				
                              WT_EXECUTEINTIMERTHREAD);	
#elif defined(__APPLE__) && defined(__MACH__)
        // Mac OS X
{'fraction_non_alphanumeric': 0.11274746499275712, 'fraction_numerical': 0.013037180106228875, 'mean_word_length': 2.509106310885218, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2101525', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2679, 'n_words': 572}