aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/hdr_histogram/src/hdr_thread.c
blob: 721780edb9e10cba44c61a51b0205abfef55ec18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* hdr_thread.c
* Written by Philip Orwig and released to the public domain,
* as explained at http://creativecommons.org/publicdomain/zero/1.0/
*/

#include <stdlib.h>
#include "hdr_thread.h"

struct hdr_mutex* hdr_mutex_alloc(void)
{
    return malloc(sizeof(hdr_mutex));
}

void hdr_mutex_free(struct hdr_mutex* mutex)
{
    free(mutex);
}

#if defined(_WIN32) || defined(_WIN64) || defined(__CYGWIN__)

#if !defined(WIN32_LEAN_AND_MEAN)
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <WinSock2.h>

int hdr_mutex_init(struct hdr_mutex* mutex)
{
    InitializeCriticalSection((CRITICAL_SECTION*)(mutex->_critical_section));
    return 0;
}

void hdr_mutex_destroy(struct hdr_mutex* mutex)
{
    DeleteCriticalSection((CRITICAL_SECTION*)(mutex->_critical_section));
}

void hdr_mutex_lock(struct hdr_mutex* mutex)
{
    EnterCriticalSection((CRITICAL_SECTION*)(mutex->_critical_section));
}

void hdr_mutex_unlock(struct hdr_mutex* mutex)
{
    LeaveCriticalSection((CRITICAL_SECTION*)(mutex->_critical_section));
}

void hdr_yield()
{
    Sleep(0);
}

int hdr_usleep(unsigned int useconds)
{
    struct timeval tv;

    tv.tv_sec = (long)useconds / 1000000;
    tv.tv_usec = useconds % 1000000;
    select(0, NULL, NULL, NULL, &tv);

    return 0;
}


#else
#include <pthread.h>
#include <unistd.h>

int hdr_mutex_init(struct hdr_mutex* mutex)
{
    return pthread_mutex_init(&mutex->_mutex, NULL);
}

void hdr_mutex_destroy(struct hdr_mutex* mutex)
{
    pthread_mutex_destroy(&mutex->_mutex);
}

void hdr_mutex_lock(struct hdr_mutex* mutex)
{
    pthread_mutex_lock(&mutex->_mutex);
}

void hdr_mutex_unlock(struct hdr_mutex* mutex)
{
    pthread_mutex_unlock(&mutex->_mutex);
}

void hdr_yield()
{
    sched_yield();
}

int hdr_usleep(unsigned int useconds)
{
    return usleep(useconds);
}


#endif