-
Notifications
You must be signed in to change notification settings - Fork 0
/
Porting.pas
162 lines (144 loc) · 3.96 KB
/
Porting.pas
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
UNIT Porting;
INTERFACE
{$IFDEF UNIX} {$DEFINE USE_PTHREADS} {$ENDIF}
uses UnixType,CTypes
{$IFDEF USE_PTHREADS},PThreads{$ENDIF}
;
function po_unixtimenow:Int64; inline;
procedure po_monotonicnanoclocks(out seconds:Int64; out nano:LongWord);
procedure po_condwait(var v: pthread_cond_t; var m:pthread_mutex_t; DelayMS: LongWord);
(*** ConditionEvent ***)
type tConditionEventLock=class
public
constructor Create;
destructor Destroy; override;
procedure WaitLock(timeout_ms: LongWord);
procedure Unlock;
procedure Lock;
procedure SignalUnlock;
{$IFDEF USE_PTHREADS} protected
mutex:pthread_mutex_t;
condv:pthread_cond_t;
{$ELSE}protected
cs:System.TRTLCriticalSection;
ev:System.PRTLEVENT;
{$ENDIF}
end;
{$packrecords C}
type
Piovec = ^Tiovec;
Tiovec = record
base : pointer;
len : size_t;
end;
Pmsghdr = ^tmsghdr;
Tmsghdr = record
name : pointer;
namelen : socklen_t;
iov : piovec;
iovlen : size_t;
control : pointer;
controllen : socklen_t;
flags : cInt;
end;
Pcmsghdr = ^Tcmsghdr;
Tcmsghdr = record
len : csize_t;
level : cInt;
name : cInt;
val : cInt;
end;
function sendmsg(__fd: cInt; __message: pmsghdr; __flags: cInt): ssize_t; cdecl; external name 'sendmsg';
function recvmsg(__fd: cInt; __message: pmsghdr; __flags: cInt): ssize_t; cdecl; external name 'recvmsg';
function cmsg_nxthdr(__mhdr:Pmsghdr;__cmsg:Pcmsghdr):Pcmsghdr; cdecl; external name '__cmsg_nxthdr';
IMPLEMENTATION
uses BaseUnix;
function po_unixtimenow:Int64;
begin
result:=fptime;
end;
Const
// Taken from linux/time.h
{$IFDEF LINUX}
CLOCK_REALTIME = 0;
CLOCK_MONOTONIC = 1;
CLOCK_PROCESS_CPUTIME_ID = 2;
CLOCK_THREAD_CPUTIME_ID = 3;
CLOCK_MONOTONIC_RAW = 4;
CLOCK_REALTIME_COARSE = 5;
CLOCK_MONOTONIC_COARSE = 6;
{$ENDIF}
function clock_gettime(clk_id : cint; tp: ptimespec) : cint; cdecl; external name 'clock_gettime';
procedure po_monotonicnanoclocks(out seconds:Int64; out nano:LongWord);
var time:timespec;
begin
assert(clock_gettime(CLOCK_MONOTONIC,@time)=0);
seconds:=time.tv_sec;
nano:=time.tv_nsec;
end;
procedure po_condwait(var v: pthread_cond_t; var m:pthread_mutex_t; DelayMS: LongWord);
var abstime:timespec;
begin
clock_gettime(CLOCK_REALTIME, @abstime);
abstime.tv_sec:=abstime.tv_sec+(DelayMS div 1000); {add seconds to sec}
DelayMS:=DelayMS mod 1000; {remove seconds from b}
abstime.tv_nsec:=abstime.tv_nsec+(DelayMS*1000000);{add milis to nsec}
if abstime.tv_nsec>1000000000 then begin
abstime.tv_nsec:=abstime.tv_nsec-1000000000;
abstime.tv_sec:=abstime.tv_sec+1;
end;
pthread_cond_timedwait(@v, @m, @abstime);
end;
constructor TConditionEventLock.Create;
{$IFDEF USE_PTHREADS} begin
pthread_mutex_init(@mutex,nil);
pthread_cond_init(@condv,nil);
{$ELSE} begin
InitCriticalSection(cs);
ev:=RTLEventCreate;
{$ENDIF}
end;
destructor TConditionEventLock.Destroy;
{$IFDEF USE_PTHREADS} begin
pthread_cond_destroy(@condv);
pthread_mutex_destroy(@mutex);
{$ELSE} begin
DoneCriticalSection(cs);
RTLeventdestroy(ev);
{$ENDIF}
end;
procedure TConditionEventLock.WaitLock(timeout_ms: LongWord);
{$IFDEF USE_PTHREADS}
begin
pthread_mutex_unlock(@mutex);
po_condwait(condv,mutex,timeout_ms);
{$ELSE} begin
RTLEventWaitFor(ev,timeout_ms);
EnterCriticalSection(cs);
RTLEventResetEvent(ev);
{$ENDIF}
end;
procedure TConditionEventLock.Unlock;
{$IFDEF USE_PTHREADS} begin
pthread_mutex_unlock(@mutex);
{$ELSE} begin
LeaveCriticalSection(cs);
{$ENDIF}
end;
procedure TConditionEventLock.Lock;
{$IFDEF USE_PTHREADS} begin
pthread_mutex_lock(@mutex);
{$ELSE} begin
EnterCriticalSection(cs);
{$ENDIF}
end;
procedure TConditionEventLock.SignalUnlock;
{$IFDEF USE_PTHREADS} begin
pthread_cond_signal(@condv);
pthread_mutex_unlock(@mutex);
{$ELSE} begin
RTLEventSetEvent(ev);
LeaveCriticalSection(cs);
{$ENDIF}
end;
END.