posixc.library: extract child creation in vfork to separate function
[AROS.git] / workbench / libs / uuid / uuid_init.c
blob5e76399be424ae7b6b6bd46fe97f2384703a2eb9
1 /*
2 Copyright © 2007-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/symbolsets.h>
7 #include <aros/debug.h>
8 #include <proto/exec.h>
9 #include <proto/dos.h>
10 #include <proto/timer.h>
11 #include <dos/var.h>
13 #include "uuid_private.h"
14 #include LC_LIBDEFS_FILE
16 static int UUID_Init(LIBBASETYPEPTR LIBBASE)
18 struct timeval tv;
19 uuid_time_t time_now;
21 /* Set up global lock (class and interface locks are separate!) */
22 InitSemaphore(&LIBBASE->uuid_GlobalLock);
24 D(bug("[UUID] UUID Init\n"));
26 LIBBASE->uuid_Initialized = 0;
28 /* I need timer.device in order to obtain system time */
29 if (OpenDevice("timer.device", UNIT_MICROHZ, &LIBBASE->uuid_TR.tr_node, 0))
31 D(bug("[UUID] Could not open timer.device. ABORT!\n"));
32 return FALSE;
35 /* get the system time and convert it to UUID time */
36 GetSysTime(&tv);
37 time_now = LIBBASE->uuid_NextUpdate = LIBBASE->uuid_LastTime =
38 ((uint64_t)tv.tv_secs + 2922) * 10000000 +
39 ((uint64_t)tv.tv_micro) * 10 +
40 ((uint64_t)0x01B21DD213814000LL);
42 D(bug("[UUID] UUID time: 0x%08lx%08lx\n",
43 (uint32_t)((LIBBASE->uuid_LastTime >> 32) & 0xffffffff),
44 (uint32_t)((LIBBASE->uuid_LastTime & 0xffffffff))
45 ));
47 /* Seed the random generator */
48 time_now /= UUIDS_PER_TICK;
49 UUIDBase->uuid_RandomSeed = (time_now >> 32) ^ time_now;
50 UUIDBase->uuid_UUIDs_ThisTick = 0;
52 /* Try to open dos.library for GetVar/SetVar */
53 DOSBase = (void *)OpenLibrary("dos.library", 0);
54 if (DOSBase)
56 D(bug("[UUID] dos.library opened. Trying to get the UUID state.\n"));
58 /* DOS is there. Try to get the last UUID state. */
59 if (GetVar("uuid_state", (UBYTE*)&LIBBASE->uuid_State, sizeof(uuid_state_t),
60 GVF_BINARY_VAR | GVF_DONT_NULL_TERM) == sizeof(uuid_state_t))
62 D(bug("[UUID] got last UUID state\n"));
63 LIBBASE->uuid_Initialized = 1;
65 else
67 D(bug("[UUID] no UUID state found. Staying uninitlaized\n"));
70 else
71 D(bug("[UUID] dos.library not yet available. I will try later."));
73 return TRUE;
76 static int UUID_Expunge(LIBBASETYPEPTR LIBBASE)
78 D(bug("[UUID] Expunge. Byebye.\n"));
80 CloseDevice(&LIBBASE->uuid_TR.tr_node);
82 if (DOSBase)
84 /* Store the state */
85 SetVar("uuid_state", (UBYTE*)&LIBBASE->uuid_State, sizeof(uuid_state_t),
86 GVF_GLOBAL_ONLY | GVF_SAVE_VAR | LV_VAR);
88 CloseLibrary((void *)DOSBase);
91 return TRUE;
94 ADD2INITLIB(UUID_Init, 0)
95 ADD2EXPUNGELIB(UUID_Expunge, 0)