📄 sync.c
字号:
static int ProcSyncDispatch(client) ClientPtr client;{ REQUEST(xReq); switch (stuff->data) { case X_SyncInitialize: return ProcSyncInitialize(client); case X_SyncListSystemCounters: return ProcSyncListSystemCounters(client); case X_SyncCreateCounter: return ProcSyncCreateCounter(client); case X_SyncSetCounter: return ProcSyncSetCounter(client); case X_SyncChangeCounter: return ProcSyncChangeCounter(client); case X_SyncQueryCounter: return ProcSyncQueryCounter(client); case X_SyncDestroyCounter: return ProcSyncDestroyCounter(client); case X_SyncAwait: return ProcSyncAwait(client); case X_SyncCreateAlarm: return ProcSyncCreateAlarm(client); case X_SyncChangeAlarm: return ProcSyncChangeAlarm(client); case X_SyncQueryAlarm: return ProcSyncQueryAlarm(client); case X_SyncDestroyAlarm: return ProcSyncDestroyAlarm(client); case X_SyncSetPriority: return ProcSyncSetPriority(client); case X_SyncGetPriority: return ProcSyncGetPriority(client); default: return BadRequest; }}/* * Boring Swapping stuff ... */static int SProcSyncInitialize(client) ClientPtr client;{ REQUEST(xSyncInitializeReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncInitializeReq); return ProcSyncInitialize(client);}static int SProcSyncListSystemCounters(client) ClientPtr client;{ REQUEST(xSyncListSystemCountersReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncListSystemCountersReq); return ProcSyncListSystemCounters(client);}static int SProcSyncCreateCounter(client) ClientPtr client;{ REQUEST(xSyncCreateCounterReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncCreateCounterReq); swapl(&stuff->cid, n); swapl(&stuff->initial_value_lo, n); swapl(&stuff->initial_value_hi, n); return ProcSyncCreateCounter(client);}static int SProcSyncSetCounter(client) ClientPtr client;{ REQUEST(xSyncSetCounterReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncSetCounterReq); swapl(&stuff->cid, n); swapl(&stuff->value_lo, n); swapl(&stuff->value_hi, n); return ProcSyncSetCounter(client);}static int SProcSyncChangeCounter(client) ClientPtr client;{ REQUEST(xSyncChangeCounterReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncChangeCounterReq); swapl(&stuff->cid, n); swapl(&stuff->value_lo, n); swapl(&stuff->value_hi, n); return ProcSyncChangeCounter(client);}static int SProcSyncQueryCounter(client) ClientPtr client;{ REQUEST(xSyncQueryCounterReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncQueryCounterReq); swapl(&stuff->counter, n); return ProcSyncQueryCounter(client);}static int SProcSyncDestroyCounter(client) ClientPtr client;{ REQUEST(xSyncDestroyCounterReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncDestroyCounterReq); swapl(&stuff->counter, n); return ProcSyncDestroyCounter(client);}static int SProcSyncAwait(client) ClientPtr client;{ REQUEST(xSyncAwaitReq); register char n; swaps(&stuff->length, n); REQUEST_AT_LEAST_SIZE(xSyncAwaitReq); SwapRestL(stuff); return ProcSyncAwait(client);}static int SProcSyncCreateAlarm(client) ClientPtr client;{ REQUEST(xSyncCreateAlarmReq); register char n; swaps(&stuff->length, n); REQUEST_AT_LEAST_SIZE(xSyncCreateAlarmReq); swapl(&stuff->id, n); swapl(&stuff->valueMask, n); SwapRestL(stuff); return ProcSyncCreateAlarm(client);}static int SProcSyncChangeAlarm(client) ClientPtr client;{ REQUEST(xSyncChangeAlarmReq); register char n; swaps(&stuff->length, n); REQUEST_AT_LEAST_SIZE(xSyncChangeAlarmReq); swapl(&stuff->alarm, n); swapl(&stuff->valueMask, n); SwapRestL(stuff); return ProcSyncChangeAlarm(client);}static int SProcSyncQueryAlarm(client) ClientPtr client;{ REQUEST(xSyncQueryAlarmReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncQueryAlarmReq); swapl(&stuff->alarm, n); return ProcSyncQueryAlarm(client);}static int SProcSyncDestroyAlarm(client) ClientPtr client;{ REQUEST(xSyncDestroyAlarmReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncDestroyAlarmReq); swapl(&stuff->alarm, n); return ProcSyncDestroyAlarm(client);}static int SProcSyncSetPriority(client) ClientPtr client;{ REQUEST(xSyncSetPriorityReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncSetPriorityReq); swapl(&stuff->id, n); swapl(&stuff->priority, n); return ProcSyncSetPriority(client);}static int SProcSyncGetPriority(client) ClientPtr client;{ REQUEST(xSyncGetPriorityReq); register char n; swaps(&stuff->length, n); REQUEST_SIZE_MATCH (xSyncGetPriorityReq); swapl(&stuff->id, n); return ProcSyncGetPriority(client);}static int SProcSyncDispatch(client) ClientPtr client;{ REQUEST(xReq); switch (stuff->data) { case X_SyncInitialize: return SProcSyncInitialize(client); case X_SyncListSystemCounters: return SProcSyncListSystemCounters(client); case X_SyncCreateCounter: return SProcSyncCreateCounter(client); case X_SyncSetCounter: return SProcSyncSetCounter(client); case X_SyncChangeCounter: return SProcSyncChangeCounter(client); case X_SyncQueryCounter: return SProcSyncQueryCounter(client); case X_SyncDestroyCounter: return SProcSyncDestroyCounter(client); case X_SyncAwait: return SProcSyncAwait(client); case X_SyncCreateAlarm: return SProcSyncCreateAlarm(client); case X_SyncChangeAlarm: return SProcSyncChangeAlarm(client); case X_SyncQueryAlarm: return SProcSyncQueryAlarm(client); case X_SyncDestroyAlarm: return SProcSyncDestroyAlarm(client); case X_SyncSetPriority: return SProcSyncSetPriority(client); case X_SyncGetPriority: return SProcSyncGetPriority(client); default: return BadRequest; }}/* * Event Swapping */static void SCounterNotifyEvent(from, to) xSyncCounterNotifyEvent *from, *to;{ to->type = from->type; to->kind = from->kind; cpswaps(from->sequenceNumber, to->sequenceNumber); cpswapl(from->counter, to->counter); cpswapl(from->wait_value_lo, to->wait_value_lo); cpswapl(from->wait_value_hi, to->wait_value_hi); cpswapl(from->counter_value_lo, to->counter_value_lo); cpswapl(from->counter_value_hi, to->counter_value_hi); cpswapl(from->time, to->time); cpswaps(from->count, to->count); to->destroyed = from->destroyed;}static void SAlarmNotifyEvent(from, to) xSyncAlarmNotifyEvent *from, *to;{ to->type = from->type; to->kind = from->kind; cpswaps(from->sequenceNumber, to->sequenceNumber); cpswapl(from->alarm, to->alarm); cpswapl(from->counter_value_lo, to->counter_value_lo); cpswapl(from->counter_value_hi, to->counter_value_hi); cpswapl(from->alarm_value_lo, to->alarm_value_lo); cpswapl(from->alarm_value_hi, to->alarm_value_hi); cpswapl(from->time, to->time); to->state = from->state;}/* * ** Close everything down. ** This is fairly simple for now. *//* ARGSUSED */static void SyncResetProc(extEntry) ExtensionEntry *extEntry;{ xfree(SysCounterList); SysCounterList = NULL; RTCounter = 0;}/* * ** Initialise the extension. */void SyncExtensionInit(){ ExtensionEntry *extEntry; if (RTCounter == 0) { RTCounter = CreateNewResourceType(FreeCounter); } RTAlarm = CreateNewResourceType(FreeAlarm); RTAwait = CreateNewResourceType(FreeAwait)|RC_NEVERRETAIN; RTAlarmClient = CreateNewResourceType(FreeAlarmClient)|RC_NEVERRETAIN; if (RTCounter == 0 || RTAwait == 0 || RTAlarm == 0 || RTAlarmClient == 0 || (extEntry = AddExtension(SYNC_NAME, XSyncNumberEvents, XSyncNumberErrors, ProcSyncDispatch, SProcSyncDispatch, SyncResetProc, StandardMinorOpcode)) == NULL) { ErrorF("Sync Extension %d.%d failed to Initialise\n", SYNC_MAJOR_VERSION, SYNC_MINOR_VERSION); return; } SyncReqCode = extEntry->base; SyncEventBase = extEntry->eventBase; SyncErrorBase = extEntry->errorBase; EventSwapVector[SyncEventBase + XSyncCounterNotify] = (EventSwapPtr) SCounterNotifyEvent; EventSwapVector[SyncEventBase + XSyncAlarmNotify] = (EventSwapPtr) SAlarmNotifyEvent; /* * Although SERVERTIME is implemented by the OS layer, we initialise it * here because doing it in OsInit() is too early. The resource database * is not initialised when OsInit() is called. This is just about OK * because there is always a servertime counter. */ SyncInitServerTime();#ifdef DEBUG fprintf(stderr, "Sync Extension %d.%d\n", SYNC_MAJOR_VERSION, SYNC_MINOR_VERSION);#endif}/* * ***** SERVERTIME implementation - should go in its own file in OS directory? */#if !defined(WIN32) && !defined(MINIX) && !defined(Lynx)#include <sys/time.h>#endifstatic pointer ServertimeCounter;static XSyncValue Now;static XSyncValue *pnext_time;#define GetTime()\{\ unsigned long millis = GetTimeInMillis();\ unsigned long maxis = XSyncValueHigh32(Now);\ if (millis < XSyncValueLow32(Now)) maxis++;\ XSyncIntsToValue(&Now, millis, maxis);\}/**** Server Block Handler*** code inspired by multibuffer extension *//*ARGSUSED*/static void ServertimeBlockHandler(env, wt, LastSelectMask)pointer env;struct timeval **wt;pointer LastSelectMask;{ XSyncValue delay; unsigned long timeout; if (pnext_time) { GetTime(); if (XSyncValueGreaterOrEqual(Now, *pnext_time)) { timeout = 0; } else { Bool overflow; XSyncValueSubtract(&delay, *pnext_time, Now, &overflow); timeout = XSyncValueLow32(delay); } AdjustWaitForDelay(wt, timeout); /* os/utils.c */ }}/**** Wakeup Handler *//*ARGSUSED*/static void ServertimeWakeupHandler(env, rc, LastSelectMask)pointer env;int rc;pointer LastSelectMask;{ if (pnext_time) { GetTime(); if (XSyncValueGreaterOrEqual(Now, *pnext_time)) { SyncChangeCounter(ServertimeCounter, Now); } }}static voidServertimeQueryValue(pCounter, pValue_return) pointer pCounter; CARD64 *pValue_return;{ GetTime(); *pValue_return = Now;}static voidServertimeBracketValues(pCounter, pbracket_less, pbracket_greater) pointer pCounter; CARD64 *pbracket_less; CARD64 *pbracket_greater;{ if (!pnext_time && pbracket_greater) { RegisterBlockAndWakeupHandlers(ServertimeBlockHandler, ServertimeWakeupHandler, NULL); } else if (pnext_time && !pbracket_greater) { RemoveBlockAndWakeupHandlers(ServertimeBlockHandler, ServertimeWakeupHandler, NULL); } pnext_time = pbracket_greater;}static voidSyncInitServerTime(){ CARD64 resolution; XSyncIntsToValue(&Now, GetTimeInMillis(), 0); XSyncIntToValue(&resolution, 4); ServertimeCounter = SyncCreateSystemCounter("SERVERTIME", Now, resolution, XSyncCounterNeverDecreases, ServertimeQueryValue, ServertimeBracketValues); pnext_time = NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -