<commit_before>// Module:  Log4CPLUS
// File:    threads.cxx
// Created: 6/2001
// Author:  <NAME>
//
//
// Copyright (C) The Apache Software Foundation. All rights reserved.
//
// This software is published under the terms of the Apache Software
// License version 1.1, a copy of which has been included with this
// distribution in the LICENSE.APL file.
//

#include <log4cplus/helpers/loglog.h>
#include <log4cplus/helpers/threads.h>
#include <exception>
#include <sstream>
#include <stdexcept>
#include <errno.h>
#include <time.h>

#ifdef LOG4CPLUS_USE_PTHREADS
#    include <sched.h>
#endif

using namespace log4cplus::helpers;


#ifndef LOG4CPLUS_SINGLE_THREADED


LOG4CPLUS_MUTEX_PTR_DECLARE 
log4cplus::thread::createNewMutex()
{
#ifdef LOG4CPLUS_USE_PTHREADS
    pthread_mutex_t* m = new pthread_mutex_t();
    pthread_mutex_init(m, NULL);
#endif
#ifdef LOG4CPLUS_USE_WIN32_THREADS
    CRITICAL_SECTION* m = new CRITICAL_SECTION();
    InitializeCriticalSection(m);
#endif
    return m;
}


void 
log4cplus::thread::deleteMutex(LOG4CPLUS_MUTEX_PTR_DECLARE m)
{
#ifdef LOG4CPLUS_USE_PTHREADS
    pthread_mutex_destroy(m);
#endif
#ifdef LOG4CPLUS_USE_WIN32_THREADS
    DeleteCriticalSection(m);
#endif
    delete m;
}



#ifdef LOG4CPLUS_USE_PTHREADS
pthread_key_t*
log4cplus::thread::createPthreadKey()
{
    pthread_key_t* key = new pthread_key_t();
    pthread_key_create(key, NULL);
    return key;
}
#endif


log4cplus::thread::Guard::~Guard()
{
    try {
        LOG4CPLUS_MUTEX_UNLOCK( _mutex );
    }
    catch(exception& e) {
        helpers::getLogLog().error(std::string("**** Guard::~Guard() exception: ") + e.what());
        throw;
    }
    catch(...) {
        helpers::getLogLog().error("**** Guard::~Guard() exception!!!!");
        throw;
    }
}



void
log4cplus::thread::yield()
{
#ifdef LOG4CPLUS_USE_PTHREADS
    sched_yield();
#endif
#ifdef LOG4CPLUS_USE_WIN32_THREADS
    Sleep(0);
#endif
}


#define MILLIS_TO_NANOS 1000000
#define SEC_TO_MILLIS 1000
#define MAX_SECS_HELD_IN_DWORD (DWORD)4294966        // 2**32/1000

void
log4cplus::thread::sleep(unsigned long secs, unsigned long nanosecs)
{
#ifdef LOG4CPLUS_USE_PTHREADS
    timespec sleeptime = { secs, nanosecs };
    timespec remain;
    while (nanosleep(&sleeptime, &remain)) {
        if (errno == EINTR) {
            sleeptime.tv_sec  = remain.tv_sec;
            sleeptime.tv_nsec = remain.tv_nsec;               
            continue;
        }
        else {
            return;
        }
    }
#endif
#ifdef LOG4CPLUS_USE_WIN32_THREADS
    // Sleep for the specified number of nano seconds
    Sleep(nanosecs / static_cast<unsigned long>(MILLIS_TO_NANOS));
    
    // Now sleep for the specified number of seconds.  This may mean we
    // have to enter a loop if secs converted to milliseconds is greater
    // than what a DWORD can hold
    DWORD numSleepLoops = secs / MAX_SECS_HELD_IN_DWORD;
    for(DWORD i=0; i<numSleepLoops; ++i) {
        Sleep(MAX_SECS_HELD_IN_DWORD * SEC_TO_MILLIS);
    }
               
    Sleep((secs % MAX_SECS_HELD_IN_DWORD) * SEC_TO_MILLIS);
#endif
}<commit_msg>Fixed sleep() method.<commit_after>// Module:  Log4CPLUS
// File:    threads.cxx
// Created: 6/2001
// Author:  <NAME>
//
//
// Copyright (C) The Apache Software Foundation. All rights reserved.
//
// This software is published under the terms of the Apache Software
// License version 1.1, a copy of which has been included with this
// distribution in the LICENSE.APL file.
//

#include <log4cplus/helpers/loglog.h>
#include <log4cplus/helpers/threads.h>
#include <exception>
#include <sstream>
#include <stdexcept>
#include <errno.h>
#include <time.h>

#ifdef LOG4CPLUS_USE_PTHREADS
#    include <sched.h>
#endif

using namespace log4cplus::helpers;


#ifndef LOG4CPLUS_SINGLE_THREADED


LOG4CPLUS_MUTEX_PTR_DECLARE 
log4cplus::thread::createNewMutex()
{
#ifdef LOG4CPLUS_USE_PTHREADS
    pthread_mutex_t* m = new pthread_mutex_t();
    pthread_mutex_init(m, NULL);
#endif
#ifdef LOG4CPLUS_USE_WIN32_THREADS
    CRITICAL_SECTION* m = new CRITICAL_SECTION();
    InitializeCriticalSection(m);
#endif
    return m;
}


void 
log4cplus::thread::deleteMutex(LOG4CPLUS_MUTEX_PTR_DECLARE m)
{
#ifdef LOG4CPLUS_USE_PTHREADS
    pthread_mutex_destroy(m);
#endif
#ifdef LOG4CPLUS_USE_WIN32_THREADS
    DeleteCriticalSection(m);
#endif
    delete m;
}



#ifdef LOG4CPLUS_USE_PTHREADS
pthread_key_t*
log4cplus::thread::createPthreadKey()
{
    pthread_key_t* key = new pthread_key_t();
    pthread_key_create(key, NULL);
    return key;
}
#endif


log4cplus::thread::Guard::~Guard()
{
    try {
        LOG4CPLUS_MUTEX_UNLOCK( _mutex );
    }
    catch(exception& e) {
        helpers::getLogLog().error(std::string("**** Guard::~Guard() exception: ") + e.what());
        throw;
    }
    catch(...) {
        helpers::getLogLog().error("**** Guard::~Guard() exception!!!!");
        throw;
    }
}



void
log4cplus::thread::yield()
{
#ifdef LOG4CPLUS_USE_PTHREADS
    sched_yield();
#endif
#ifdef LOG4CPLUS_USE_WIN32_THREADS
    Sleep(0);
#endif
}


#define MILLIS_TO_NANOS 1000000
#define SEC_TO_MILLIS 1000
#define MAX_SLEEP_SECONDS (DWORD)4294966        // (2**32-2)/1000

void
log4cplus::thread::sleep(unsigned long secs, unsigned long nanosecs)
{
#ifdef LOG4CPLUS_USE_PTHREADS
    timespec sleep_time = { secs, nanosecs };
    timespec remain;
    while (nanosleep(&sleep_time, &remain)) {
        if (errno == EINTR) {
            sleep_time.tv_sec  = remain.tv_sec;
            sleep_time.tv_nsec = remain.tv_nsec;               
            continue;
        }
        else {
            return;
        }
    }
#endif
#ifdef LOG4CPLUS_USE_WIN32_THREADS
    DWORD nano_millis = nanosecs / static_cast<unsigned long>(MILLIS_TO_NANOS);
    if (secs <= MAX_SLEEP_SECONDS) {
        Sleep((secs * SEC_TO_MILLIS) + nano_millis);
        return;
    }
        
    DWORD no_of_max_sleeps = secs / MAX_SLEEP_SECONDS;
            
    for(DWORD i = 0; i < no_of_max_sleeps; i++) {
        Sleep(MAX_SLEEP_SECONDS * 1000);
    }
               
    Sleep((secs % MAX_SLEEP_SECONDS) * SEC_TO_MILLIS + nano_millis);
#endif
}
{'fraction_non_alphanumeric': 0.11563624468433105, 'fraction_numerical': 0.022407589139679423, 'mean_word_length': 3.408795962509012, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '6580451', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2268, 'n_words': 454}