/
core.d
989 lines (835 loc) · 25.5 KB
/
core.d
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
/**
This module contains the core functionality of the vibe framework.
Copyright: © 2012-2013 RejectedSoftware e.K.
License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
Authors: Sönke Ludwig
*/
module vibe.core.core;
public import vibe.core.driver;
import vibe.core.args;
import vibe.core.concurrency;
import vibe.core.log;
import vibe.utils.array;
import std.algorithm;
import std.conv;
import std.encoding;
import std.exception;
import std.functional;
import std.range;
import std.string;
import std.variant;
import core.sync.mutex;
import core.stdc.stdlib;
import core.thread;
version(VibeLibevDriver) import vibe.core.drivers.libev;
version(VibeLibeventDriver) import vibe.core.drivers.libevent2;
version(VibeWin32Driver) import vibe.core.drivers.win32;
version(VibeWinrtDriver) import vibe.core.drivers.winrt;
version(Posix)
{
import core.sys.posix.signal;
import core.sys.posix.unistd;
import core.sys.posix.pwd;
static if (__traits(compiles, {import core.sys.posix.grp; getgrgid(0);})) {
import core.sys.posix.grp;
} else {
extern (C) {
struct group {
char* gr_name;
char* gr_passwd;
gid_t gr_gid;
char** gr_mem;
}
group* getgrgid(gid_t);
group* getgrnam(in char*);
}
}
}
version (Windows)
{
import core.stdc.signal;
}
/**************************************************************************************************/
/* Public functions */
/**************************************************************************************************/
/**
Starts the vibe event loop.
Note that this function is usually called automatically by the vibe framework. However, if
you provide your own main() function, you need to call it manually.
The event loop will continue running during the whole life time of the application.
Tasks will be started and handled from within the event loop.
*/
int runEventLoop()
{
s_eventLoopRunning = true;
scope(exit) s_eventLoopRunning = false;
// runs any yield()ed tasks first
s_core.m_exit = false;
s_core.notifyIdle();
if (s_core.m_exit) return 0;
if( auto err = getEventDriver().runEventLoop() != 0){
if( err == 1 ){
logDebug("No events active, exiting message loop.");
return 0;
}
logError("Error running event loop: %d", err);
return 1;
}
return 0;
}
/**
Stops the currently running event loop.
Calling this function will cause the event loop to stop event processing and
the corresponding call to runEventLoop() will return to its caller.
*/
void exitEventLoop(bool shutdown_workers = true)
{
assert(s_eventLoopRunning);
if (shutdown_workers && st_workerTaskMutex) {
synchronized (st_workerTaskMutex)
foreach (ref ctx; st_workerThreads)
ctx.exit = true;
st_workerTaskSignal.emit();
while (true) {
synchronized (st_workerTaskMutex)
if (st_workerThreads.length == 0)
break;
st_workerTaskSignal.wait();
}
}
getEventDriver().exitEventLoop();
}
/**
Process all pending events without blocking.
Checks if events are ready to trigger immediately, and run their callbacks if so.
Returns: Returns false iff exitEventLoop was called in the process.
*/
bool processEvents()
{
return getEventDriver().processEvents();
}
/**
Sets a callback that is called whenever no events are left in the event queue.
The callback delegate is called whenever all events in the event queue have been
processed. Returning true from the callback will cause another idle event to
be triggered immediately after processing any events that have arrived in the
meantime. Returning fals will instead wait until another event has arrived first.
*/
void setIdleHandler(void delegate() del)
{
s_idleHandler = { del(); return false; };
}
/// ditto
void setIdleHandler(bool delegate() del)
{
s_idleHandler = del;
}
/**
Runs a new asynchronous task.
task will be called synchronously from within the vibeRunTask call. It will
continue to run until vibeYield() or any of the I/O or wait functions is
called.
*/
Task runTask(void delegate() task)
{
CoreTask f;
while (s_availableFibersCount > 0) {
// pick the first available fiber
f = s_availableFibers[--s_availableFibersCount];
if (f.state == Fiber.State.TERM) {
f = null;
continue;
}
}
if (f is null) {
// if there is no fiber available, create one.
if( s_availableFibers.length == 0 ) s_availableFibers.length = 1024;
logDebug("Creating new fiber...");
s_fiberCount++;
f = new CoreTask;
}
f.m_taskFunc = task;
f.m_taskCounter++;
auto handle = f.task();
logTrace("initial task call (%s)", f.state);
debug if (s_taskEventCallback) s_taskEventCallback(TaskEvent.preStart);
s_core.resumeTask(handle, null, true);
logTrace("run task out");
return handle;
}
/**
Runs a new asynchronous task in a worker thread.
Only function pointers with weakly isolated arguments are allowed to be
able to guarantee thread-safety.
*/
void runWorkerTask(R, ARGS...)(R function(ARGS) func, ARGS args)
{
foreach (T; ARGS) static assert(isWeaklyIsolated!T, "Argument type "~T.stringof~" is not safe to pass between threads.");
runWorkerTask_unsafe({ func(args); });
}
/// ditto
void runWorkerTask(alias method, T, ARGS...)(shared(T) object, ARGS args)
{
foreach (T; ARGS) static assert(isWeaklyIsolated!T, "Argument type "~T.stringof~" is not safe to pass between threads.");
runWorkerTask_unsafe({ object.method(args); });
}
private void runWorkerTask_unsafe(void delegate() del)
{
if (st_workerTaskMutex) {
synchronized (st_workerTaskMutex) {
st_workerTasks ~= del;
}
st_workerTaskSignal.emit();
} else {
runTask(del);
}
}
/**
Runs a new asynchronous task in all worker threads concurrently.
This function is mainly useful for long-living tasks that distribute their
work across all CPU cores. Only function pointers with weakly isolated
arguments are allowed to be able to guarantee thread-safety.
*/
void runWorkerTaskDist(R, ARGS...)(R function(ARGS) func, ARGS args)
{
foreach (T; ARGS) static assert(isWeaklyIsolated!T, "Argument type "~T.stringof~" is not safe to pass between threads.");
runWorkerTaskDist_unsafe({ func(args); });
}
/// ditto
void runWorkerTaskDist(alias method, T, ARGS...)(shared(T) object, ARGS args)
{
foreach (T; ARGS) static assert(isWeaklyIsolated!T, "Argument type "~T.stringof~" is not safe to pass between threads.");
runWorkerTaskDist_unsafe({ object.method(args); });
}
private void runWorkerTaskDist_unsafe(void delegate() del)
{
if (st_workerTaskMutex) {
synchronized (st_workerTaskMutex) {
foreach (ref ctx; st_workerThreads)
ctx.taskQueue ~= del;
}
st_workerTaskSignal.emit();
} else {
runTask(del);
}
}
/**
Suspends the execution of the calling task to let other tasks and events be
handled.
Calling this function in short intervals is recommended if long CPU
computations are carried out by a task. It can also be used in conjunction
with Signals to implement cross-fiber events with no polling.
*/
void yield()
{
s_yieldedTasks ~= Task.getThis();
rawYield();
}
/**
Yields execution of this task until an event wakes it up again.
Beware that the task will starve if no event wakes it up.
*/
void rawYield()
{
s_core.yieldForEvent();
}
/**
Suspends the execution of the calling task for the specified amount of time.
*/
void sleep(Duration timeout)
{
auto tm = getEventDriver().createTimer(null);
tm.rearm(timeout);
tm.wait();
}
/**
Returns a new armed timer.
Params:
timeout = Determines the minimum amount of time that elapses before the timer fires.
callback = This delegate will be called when the timer fires
periodic = Speficies if the timer fires repeatedly or only once
Returns:
Returns a Timer object that can be used to identify and modify the timer.
*/
Timer setTimer(Duration timeout, void delegate() callback, bool periodic = false)
{
auto tm = getEventDriver().createTimer(callback);
tm.rearm(timeout, periodic);
return tm;
}
/**
Creates a new timer without arming it.
*/
Timer createTimer(void delegate() callback)
{
return getEventDriver().createTimer(callback);
}
/**
Sets a variable specific to the calling task/fiber.
Remarks:
This function also works if called from outside if a fiber. In this case, it will work
on a thread local storage.
*/
void setTaskLocal(T)(string name, T value)
{
auto self = cast(CoreTask)Fiber.getThis();
if( self ) self.set(name, value);
s_taskLocalStorageGlobal[name] = Variant(value);
}
/**
Returns a task/fiber specific variable.
Remarks:
This function also works if called from outside if a fiber. In this case, it will work
on a thread local storage.
*/
T getTaskLocal(T)(string name)
{
auto self = cast(CoreTask)Fiber.getThis();
if( self ) return self.get!T(name);
auto pvar = name in s_taskLocalStorageGlobal;
enforce(pvar !is null, "Accessing unset TLS variable '"~name~"'.");
return pvar.get!T();
}
/**
Returns a task/fiber specific variable.
Remarks:
This function also works if called from outside if a fiber. In this case, it will work
on a thread local storage.
*/
bool isTaskLocalSet(string name)
{
auto self = cast(CoreTask)Fiber.getThis();
if( self ) return self.isSet(name);
return (name in s_taskLocalStorageGlobal) !is null;
}
/**
Sets the stack size for tasks.
The default stack size is set to 16 KiB, which is sufficient for most tasks. Tuning this value
can be used to reduce memory usage for great numbers of concurrent tasks or to allow applications
with heavy stack use.
Note that this function must be called before any task is started to have an effect.
*/
void setTaskStackSize(size_t sz)
{
s_taskStackSize = sz;
}
/**
Enables multithreaded worker task processing.
This function will start up a number of worker threads that will process tasks started using
runWorkerTask(). runTask() will still execute tasks on the calling thread.
Note that this functionality is experimental right now and is not recommended for general use.
*/
void enableWorkerThreads()
{
import core.cpuid;
setupDriver();
assert(st_workerTaskMutex is null);
st_workerTaskMutex = new core.sync.mutex.Mutex;
st_workerTaskSignal = getEventDriver().createManualEvent();
foreach (i; 0 .. threadsPerCPU) {
auto thr = new Thread(&workerThreadFunc);
thr.name = format("Vibe Task Worker #%s", i);
st_workerThreads[thr] = WorkerThreadContext();
thr.start();
}
}
/**
Sets the effective user and group ID to the ones configured for privilege lowering.
This function is useful for services run as root to give up on the privileges that
they only need for initialization (such as listening on ports <= 1024 or opening
system log files).
*/
void lowerPrivileges()
{
if (!isRoot()) return;
auto uname = s_privilegeLoweringUserName;
auto gname = s_privilegeLoweringGroupName;
if (uname || gname) {
static bool tryParse(T)(string s, out T n)
{
import std.conv, std.ascii;
if (!isDigit(s[0])) return false;
n = parse!T(s);
return s.length==0;
}
int uid = -1, gid = -1;
if (uname && !tryParse(uname, uid)) uid = getUID(uname);
if (gname && !tryParse(gname, gid)) gid = getGID(gname);
setUID(uid, gid);
} else logWarn("Vibe was run as root, and no user/group has been specified for privilege lowering. Running with full permissions.");
}
/**
Sets a callback that is invoked whenever a task changes its status.
This function is useful mostly for implementing debuggers that
analyze the life time of tasks, including task switches.
*/
void setTaskEventCallback(void function(TaskEvent) func)
{
s_taskEventCallback = func;
}
/**
A version string representing the current vibe version
*/
enum VibeVersionString = "0.7.16";
/**
Implements a task local storage variable.
Task local variables, similar to thread local variables, exist separately
in each task. Consequently, they do not need any form of synchronization
when accessing them.
Note, however, that each TaskLocal variable will increase the memory footprint
of any task that uses task local storage. There is also an overhead to access
TaskLocal variables, higher than for thread local variables, but generelly
still O(n).
Notice:
FiberLocal instances MUST be declared as static/global thread-local
variables. Defining them as a temporary/stack variable will cause
crashes or data corruption!
Examples:
---
TaskLocal!string s_myString = "world";
void taskFunc()
{
assert(s_myString == "world");
s_myString = "hello";
assert(s_myString == "hello");
}
shared static this()
{
// both tasks will get independent storage for s_myString
runTask(&taskFunc);
runTask(&taskFunc);
}
---
*/
struct TaskLocal(T)
{
private {
size_t m_offset = size_t.max;
size_t m_id;
T m_initValue;
}
this(T init_val) { m_initValue = init_val; }
@disable this(this);
void opAssign(bool value) { this.storage = value; }
@property ref T storage()
{
auto fiber = cast(CoreTask)Fiber.getThis();
// lazily register in FLS storage
if (m_offset == size_t.max) {
// TODO: handle alignment properly
m_offset = CoreTask.ms_flsFill;
m_id = CoreTask.ms_flsCounter++;
CoreTask.ms_flsFill += T.sizeof;
}
// make sure the current fiber has enough FLS storage
if (fiber.m_fls.length < CoreTask.ms_flsFill) {
fiber.m_fls.length = CoreTask.ms_flsFill + 128;
fiber.m_flsInit.length = CoreTask.ms_flsCounter + 64;
}
// return (possibly default initialized) value
auto data = fiber.m_fls.ptr[m_offset .. m_offset+T.sizeof];
if (!fiber.m_flsInit[m_id]) {
fiber.m_flsInit[m_id] = true;
emplace!T(data, m_initValue);
}
return (cast(T[])data)[0];
}
alias storage this;
}
/**
High level state change events for a Task
*/
enum TaskEvent {
preStart, /// Just about to invoke the fiber which starts execution
start, /// Just about to start execution
yield, /// Temporarily paused
resume, /// Resumed from a prior yield
end, /// Ended normally
fail /// Ended with an exception
}
/**************************************************************************************************/
/* private types */
/**************************************************************************************************/
private class CoreTask : TaskFiber {
import std.bitmanip;
private {
void delegate() m_taskFunc;
Exception m_exception;
Task[] m_yielders;
// task local storage
static size_t ms_flsFill = 0; // thread-local
static size_t ms_flsCounter = 0;
BitArray m_flsInit;
void[] m_fls;
}
this()
{
super(&run, s_taskStackSize);
}
private void run()
{
try {
while(true){
while( !m_taskFunc ){
try {
s_core.yieldForEvent();
} catch( Exception e ){
logWarn("CoreTaskFiber was resumed with exception but without active task!");
logDiagnostic("Full error: %s", e.toString().sanitize());
}
}
auto task = m_taskFunc;
m_taskFunc = null;
try {
m_running = true;
scope(exit) m_running = false;
logTrace("entering task.");
debug if (s_taskEventCallback) s_taskEventCallback(TaskEvent.start);
task();
debug if (s_taskEventCallback) s_taskEventCallback(TaskEvent.end);
logTrace("exiting task.");
} catch( Exception e ){
debug if (s_taskEventCallback) s_taskEventCallback(TaskEvent.fail);
import std.encoding;
logCritical("Task terminated with uncaught exception: %s", e.msg);
logDebug("Full error: %s", e.toString().sanitize());
}
resetLocalStorage();
s_yieldedTasks ~= m_yielders;
m_yielders.length = 0;
// make the fiber available for the next task
if( s_availableFibers.length <= s_availableFibersCount )
s_availableFibers.length = 2*s_availableFibers.length;
s_availableFibers[s_availableFibersCount++] = this;
rawYield();
}
} catch(Throwable th){
logCritical("CoreTaskFiber was terminated unexpectedly: %s", th.msg);
logDiagnostic("Full error: %s", th.toString().sanitize());
}
}
override void join()
{
auto caller = Task.getThis();
assert(caller.fiber !is this, "A task cannot join itself.");
assert(caller.thread is this.thread, "Joining tasks in foreign threads is currently not supported.");
m_yielders ~= caller;
auto run_count = m_taskCounter;
if (m_running && run_count == m_taskCounter) {
s_core.resumeTask(this.task);
while (m_running && run_count == m_taskCounter) rawYield();
}
}
override void interrupt()
{
auto caller = Task.getThis();
assert(caller != this.task, "A task cannot interrupt itself.");
assert(caller.thread is this.thread, "Interrupting tasks in different threads is not yet supported.");
s_core.resumeTask(this.task, new InterruptException);
}
override void terminate()
{
assert(false, "Not implemented");
}
}
private class VibeDriverCore : DriverCore {
private {
Duration m_gcCollectTimeout;
Timer m_gcTimer;
bool m_ignoreIdleForGC = false;
bool m_exit = false;
}
private void setupGcTimer()
{
m_gcTimer = getEventDriver().createTimer(&collectGarbage);
m_gcCollectTimeout = dur!"seconds"(2);
}
void yieldForEvent()
{
auto fiber = cast(CoreTask)Fiber.getThis();
if( fiber ){
logTrace("yield");
debug if (s_taskEventCallback) s_taskEventCallback(TaskEvent.yield);
Fiber.yield();
debug if (s_taskEventCallback) s_taskEventCallback(TaskEvent.resume);
logTrace("resume");
auto e = fiber.m_exception;
if( e ){
fiber.m_exception = null;
throw e;
}
} else {
assert(!s_eventLoopRunning, "Event processing outside of a fiber should only happen before the event loop is running!?");
if( auto err = getEventDriver().runEventLoopOnce() ){
if( err == 1 ){
logDebug("No events registered, exiting event loop.");
throw new Exception("No events registered in vibeYieldForEvent.");
}
logError("Error running event loop: %d", err);
throw new Exception("Error waiting for events.");
}
}
}
void resumeTask(Task task, Exception event_exception = null)
{
resumeTask(task, event_exception, false);
}
void resumeTask(Task task, Exception event_exception, bool initial_resume)
{
assert(initial_resume || task.running, "Resuming terminated task.");
CoreTask ctask = cast(CoreTask)task.fiber;
assert(ctask.thread is Thread.getThis(), "Resuming task in foreign thread.");
assert(ctask.state == Fiber.State.HOLD, "Resuming fiber that is " ~ to!string(ctask.state));
if( event_exception ){
extrap();
ctask.m_exception = event_exception;
}
auto uncaught_exception = task.call(false);
if( uncaught_exception ){
extrap();
assert(task.state == Fiber.State.TERM);
logError("Task terminated with unhandled exception: %s", uncaught_exception.toString());
}
}
void notifyIdle()
{
bool again = true;
while (again) {
if (s_idleHandler)
again = s_idleHandler();
else again = false;
Task[] tmp;
swap(s_yieldedTasks, tmp);
foreach(t; tmp)
if (t.running)
resumeTask(t);
if (s_yieldedTasks.length > 0)
again = true;
if (again)
if (!processEvents()) {
m_exit = true;
return;
}
}
if( !m_ignoreIdleForGC && m_gcTimer ){
m_gcTimer.rearm(m_gcCollectTimeout);
} else m_ignoreIdleForGC = false;
}
private void collectGarbage()
{
import core.memory;
logTrace("gc idle collect");
GC.collect();
GC.minimize();
m_ignoreIdleForGC = true;
}
}
private struct WorkerThreadContext {
void delegate()[] taskQueue;
bool exit = false;
}
/**************************************************************************************************/
/* private functions */
/**************************************************************************************************/
private {
__gshared VibeDriverCore s_core;
__gshared size_t s_taskStackSize = 16*4096;
__gshared core.sync.mutex.Mutex st_workerTaskMutex;
__gshared void delegate()[] st_workerTasks;
__gshared WorkerThreadContext[Thread] st_workerThreads;
__gshared ManualEvent st_workerTaskSignal;
bool delegate() s_idleHandler;
__gshared debug void function(TaskEvent) s_taskEventCallback;
Task[] s_yieldedTasks;
bool s_eventLoopRunning = false;
Variant[string] s_taskLocalStorageGlobal; // for use outside of a task
CoreTask[] s_availableFibers;
size_t s_availableFibersCount;
size_t s_fiberCount;
string s_privilegeLoweringUserName;
string s_privilegeLoweringGroupName;
}
// per process setup
shared static this()
{
version(Windows){
version(VibeLibeventDriver) enum need_wsa = true;
else version(VibeWin32Driver) enum need_wsa = true;
else enum need_wsa = false;
static if (need_wsa) {
logTrace("init winsock");
// initialize WinSock2
import std.c.windows.winsock;
WSADATA data;
WSAStartup(0x0202, &data);
}
}
initializeLogModule();
logTrace("create driver core");
s_core = new VibeDriverCore;
version(Posix){
logTrace("setup signal handler");
// support proper shutdown using signals
sigset_t sigset;
sigemptyset(&sigset);
sigaction_t siginfo;
siginfo.sa_handler = &onSignal;
siginfo.sa_mask = sigset;
siginfo.sa_flags = SA_RESTART;
sigaction(SIGINT, &siginfo, null);
sigaction(SIGTERM, &siginfo, null);
siginfo.sa_handler = &onBrokenPipe;
sigaction(SIGPIPE, &siginfo, null);
}
version(Windows){
signal(SIGABRT, &onSignal);
signal(SIGTERM, &onSignal);
signal(SIGINT, &onSignal);
}
setupDriver();
version(VibeIdleCollect){
logTrace("setup gc");
s_core.setupGcTimer();
}
getOption("uid|user", &s_privilegeLoweringUserName, "Sets the user name or id used for privilege lowering.");
getOption("gid|group", &s_privilegeLoweringGroupName, "Sets the group name or id used for privilege lowering.");
}
shared static ~this()
{
bool tasks_left = false;
if(st_workerTaskMutex !is null) { //This mutex is experimentally in enableWorkerThreads
synchronized(st_workerTaskMutex){
if( !st_workerTasks.empty ) tasks_left = true;
}
} else {
if( !st_workerTasks.empty ) tasks_left = true;
}
if( !s_yieldedTasks.empty ) tasks_left = true;
if( tasks_left ) logWarn("There are still tasks running at exit.");
delete s_core;
}
// per thread setup
static this()
{
assert(s_core !is null);
setupDriver();
}
static ~this()
{
deleteEventDriver();
}
private void setupDriver()
{
if( getEventDriver() !is null ) return;
logTrace("create driver");
version(VibeWin32Driver) setEventDriver(new Win32EventDriver(s_core));
else version(VibeWinrtDriver) setEventDriver(new WinRTEventDriver(s_core));
else version(VibeLibevDriver) setEventDriver(new LibevDriver(s_core));
else version(VibeLibeventDriver) setEventDriver(new Libevent2Driver(s_core));
else static assert(false, "No event driver is available. Please specify a -version=Vibe*Driver for the desired driver.");
logTrace("driver %s created", (cast(Object)getEventDriver()).classinfo.name);
}
private void workerThreadFunc()
{
logDebug("entering worker thread");
runTask(toDelegate(&handleWorkerTasks));
logDebug("running event loop");
runEventLoop();
}
private void handleWorkerTasks()
{
logDebug("worker task enter");
yield();
auto thisthr = Thread.getThis();
logDebug("worker task loop enter");
while(true){
void delegate() t;
auto emit_count = st_workerTaskSignal.emitCount;
synchronized(st_workerTaskMutex){
logDebug("worker task check");
if (st_workerThreads[thisthr].exit) {
if (st_workerThreads[thisthr].taskQueue.length > 0)
logWarn("Worker thread shuts down with specific worker tasks left in its queue.");
if (st_workerThreads.length == 1 && st_workerTasks.length > 0)
logWarn("Worker threads shut down with worker tasks still left in the queue.");
st_workerThreads.remove(thisthr);
getEventDriver().exitEventLoop();
break;
}
if (!st_workerTasks.empty) {
logDebug("worker task got");
t = st_workerTasks.front;
st_workerTasks.popFront();
} else if (!st_workerThreads[thisthr].taskQueue.empty) {
logDebug("worker task got specific");
t = st_workerThreads[thisthr].taskQueue.front;
st_workerThreads[thisthr].taskQueue.popFront();
}
}
if (t) runTask(t);
else st_workerTaskSignal.wait(emit_count);
}
logDebug("worker task exit");
synchronized(st_workerTaskMutex)
st_workerThreads.remove(thisthr);
st_workerTaskSignal.emit();
}
private extern(C) void extrap()
nothrow {
logTrace("exception trap");
}
private extern(C) void onSignal(int signal)
nothrow {
logInfo("Received signal %d. Shutting down.", signal);
if( s_eventLoopRunning ) try exitEventLoop(); catch(Exception e) {}
else exit(1);
}
private extern(C) void onBrokenPipe(int signal)
nothrow {
logTrace("Broken pipe.");
}
version(Posix)
{
private bool isRoot() { return geteuid() == 0; }
private void setUID(int uid, int gid)
{
logInfo("Lowering privileges to uid=%d, gid=%d...", uid, gid);
if (gid >= 0) {
enforce(getgrgid(gid) !is null, "Invalid group id!");
enforce(setegid(gid) == 0, "Error setting group id!");
}
//if( initgroups(const char *user, gid_t group);
if (uid >= 0) {
enforce(getpwuid(uid) !is null, "Invalid user id!");
enforce(seteuid(uid) == 0, "Error setting user id!");
}
}
private int getUID(string name)
{
auto pw = getpwnam(name.toStringz());
enforce(pw !is null, "Unknown user name: "~name);
return pw.pw_uid;
}
private int getGID(string name)
{
auto gr = getgrnam(name.toStringz());
enforce(gr !is null, "Unknown group name: "~name);
return gr.gr_gid;
}
} else version(Windows){
private bool isRoot() { return false; }
private void setUID(int uid, int gid)
{
enforce(false, "UID/GID not supported on Windows.");
}
private int getUID(string name)
{
enforce(false, "Privilege lowering not supported on Windows.");
assert(false);
}
private int getGID(string name)
{
enforce(false, "Privilege lowering not supported on Windows.");
assert(false);
}
}