📄 sync.c
字号:
/* $XConsortium: sync.c /main/13 1996/12/16 16:51:55 rws $ *//* $XFree86: xc/programs/Xserver/Xext/sync.c,v 3.3 1997/01/18 06:53:00 dawes Exp $ *//*Copyright (c) 1991, 1993 X ConsortiumPermission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:The above copyright notice and this permission notice shall be includedin all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OROTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.Except as contained in this notice, the name of the X Consortium shallnot be used in advertising or otherwise to promote the sale, use orother dealings in this Software without prior written authorizationfrom the X Consortium.Copyright 1991, 1993 by Digital Equipment Corporation, Maynard, Massachusetts,and Olivetti Research Limited, Cambridge, England. All Rights ReservedPermission to use, copy, modify, and distribute this software and itsdocumentation for any purpose and without fee is hereby granted,provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear insupporting documentation, and that the names of Digital or Olivetti not be used in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission. Digital and Olivettimake no representations about the suitability of this softwarefor any purpose. It is provided "as is" without express or implied warranty.DIGITAL AND OLIVETTI DISCLAIM ALL WARRANTIES WITH REGARD TO THISSOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS, IN NO EVENT SHALL THEY BE LIABLE FOR ANY SPECIAL, INDIRECT ORCONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OFUSE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OROTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE ORPERFORMANCE OF THIS SOFTWARE.*/#define NEED_REPLIES#define NEED_EVENTS#include <stdio.h>#include "X.h"#include "Xproto.h"#include "Xmd.h"#include "misc.h"#include "os.h"#include "extnsionst.h"#include "dixstruct.h"#include "resource.h"#include "opaque.h"#define _SYNC_SERVER#include "sync.h"#include "syncstr.h"/* * Local Global Variables */static int SyncReqCode;static int SyncEventBase;static int SyncErrorBase;static RESTYPE RTCounter = 0;static RESTYPE RTAwait;static RESTYPE RTAlarm;static RESTYPE RTAlarmClient;static int SyncNumSystemCounters = 0;static SyncCounter **SysCounterList = NULL;#define IsSystemCounter(pCounter) \ (pCounter && (pCounter->client == NULL))/* these are all the alarm attributes that pertain to the alarm's trigger */#define XSyncCAAllTrigger \ (XSyncCACounter | XSyncCAValueType | XSyncCAValue | XSyncCATestType)static intFreeAlarm(#if NeedFunctionPrototypes pointer /* addr */, XID /* id */#endif);static intFreeAlarmClient(#if NeedFunctionPrototypes pointer /* value */, XID /* id */#endif);static intFreeAwait(#if NeedFunctionPrototypes pointer /* addr */, XID /* id */#endif);static voidServertimeBracketValues(#if NeedFunctionPrototypes pointer /* pCounter */, CARD64 * /* pbracket_less */, CARD64 * /* pbracket_greater */#endif);static voidServertimeQueryValue(#if NeedFunctionPrototypes pointer /* pCounter */, CARD64 * /* pValue_return */#endif);static voidServertimeWakeupHandler(#if NeedFunctionPrototypes pointer /* env */, int /* rc */, pointer /* LastSelectMask */#endif);static int SyncInitTrigger(#if NeedFunctionPrototypes ClientPtr /* client */, SyncTrigger * /* pTrigger */, XSyncCounter /* counter */, Mask /* changes */#endif);static voidSAlarmNotifyEvent(#if NeedFunctionPrototypes xSyncAlarmNotifyEvent * /* from */, xSyncAlarmNotifyEvent * /* to */#endif);static voidSCounterNotifyEvent(#if NeedFunctionPrototypes xSyncCounterNotifyEvent * /* from */, xSyncCounterNotifyEvent * /* to */#endif);static voidServertimeBlockHandler(#if NeedFunctionPrototypes pointer /* env */, struct timeval ** /* wt */, pointer /* LastSelectMask */#endif);static intSyncAddTriggerToCounter(#if NeedFunctionPrototypes SyncTrigger * /* pTrigger */#endif);extern voidSyncAlarmCounterDestroyed(#if NeedFunctionPrototypes SyncTrigger * /* pTrigger */#endif);static voidSyncAlarmTriggerFired(#if NeedFunctionPrototypes SyncTrigger * /* pTrigger */#endif);static voidSyncAwaitTriggerFired(#if NeedFunctionPrototypes SyncTrigger * /* pTrigger */#endif);static intSyncChangeAlarmAttributes(#if NeedFunctionPrototypes ClientPtr /* client */, SyncAlarm * /* pAlarm */, Mask /* mask */, CARD32 * /* values */#endif);static BoolSyncCheckTriggerNegativeComparison(#if NeedFunctionPrototypes SyncTrigger * /* pTrigger */, CARD64 /* oldval */#endif);static BoolSyncCheckTriggerNegativeTransition(#if NeedFunctionPrototypes SyncTrigger * /* pTrigger */, CARD64 /* oldval */#endif);static BoolSyncCheckTriggerPositiveComparison(#if NeedFunctionPrototypes SyncTrigger * /* pTrigger */, CARD64 /* oldval */#endif);static BoolSyncCheckTriggerPositiveTransition(#if NeedFunctionPrototypes SyncTrigger * /* pTrigger */, CARD64 /* oldval */#endif);static SyncCounter *SyncCreateCounter(#if NeedFunctionPrototypes ClientPtr /* client */, XSyncCounter /* id */, CARD64 /* initialvalue */#endif);static void SyncComputeBracketValues(#if NeedFunctionPrototypes SyncCounter * /* pCounter */, Bool /* startOver */#endif);static voidSyncDeleteTriggerFromCounter(#if NeedFunctionPrototypes SyncTrigger * /* pTrigger */#endif);static BoolSyncEventSelectForAlarm(#if NeedFunctionPrototypes SyncAlarm * /* pAlarm */, ClientPtr /* client */, Bool /* wantevents */#endif);static voidSyncInitServerTime(#if NeedFunctionPrototypes void#endif);static void SyncResetProc(#if NeedFunctionPrototypes ExtensionEntry * /* extEntry */#endif);static voidSyncSendAlarmNotifyEvents(#if NeedFunctionPrototypes SyncAlarm * /* pAlarm */#endif);static voidSyncSendCounterNotifyEvents(#if NeedFunctionPrototypes ClientPtr /* client */, SyncAwait ** /* ppAwait */, int /* num_events */#endif);static DISPATCH_PROC(ProcSyncAwait);static DISPATCH_PROC(ProcSyncChangeAlarm);static DISPATCH_PROC(ProcSyncChangeCounter);static DISPATCH_PROC(ProcSyncCreateAlarm);static DISPATCH_PROC(ProcSyncCreateCounter);static DISPATCH_PROC(ProcSyncDestroyAlarm);static DISPATCH_PROC(ProcSyncDestroyCounter);static DISPATCH_PROC(ProcSyncDispatch);static DISPATCH_PROC(ProcSyncGetPriority);static DISPATCH_PROC(ProcSyncInitialize);static DISPATCH_PROC(ProcSyncListSystemCounters);static DISPATCH_PROC(ProcSyncListSystemCounters);static DISPATCH_PROC(ProcSyncQueryAlarm);static DISPATCH_PROC(ProcSyncQueryCounter);static DISPATCH_PROC(ProcSyncSetCounter);static DISPATCH_PROC(ProcSyncSetPriority);static DISPATCH_PROC(SProcSyncAwait);static DISPATCH_PROC(SProcSyncChangeAlarm);static DISPATCH_PROC(SProcSyncChangeCounter);static DISPATCH_PROC(SProcSyncCreateAlarm);static DISPATCH_PROC(SProcSyncCreateCounter);static DISPATCH_PROC(SProcSyncDestroyAlarm);static DISPATCH_PROC(SProcSyncDestroyCounter);static DISPATCH_PROC(SProcSyncDispatch);static DISPATCH_PROC(SProcSyncDispatch);static DISPATCH_PROC(SProcSyncGetPriority);static DISPATCH_PROC(SProcSyncInitialize);static DISPATCH_PROC(SProcSyncListSystemCounters);static DISPATCH_PROC(SProcSyncQueryAlarm);static DISPATCH_PROC(SProcSyncQueryCounter);static DISPATCH_PROC(SProcSyncSetCounter);static DISPATCH_PROC(SProcSyncSetPriority);/* Each counter maintains a simple linked list of triggers that are * interested in the counter. The two functions below are used to * delete and add triggers on this list. */static voidSyncDeleteTriggerFromCounter(pTrigger) SyncTrigger *pTrigger;{ SyncTriggerList *pCur, *pPrev = NULL; /* pCounter needs to be stored in pTrigger before calling here. */ if (!pTrigger->pCounter) return; for (pCur = pTrigger->pCounter->pTriglist; pCur; pCur = pCur->next) { if (pCur->pTrigger == pTrigger) { if (pPrev) pPrev->next = pCur->next; else pTrigger->pCounter->pTriglist = pCur->next; xfree(pCur); break; } } if (IsSystemCounter(pTrigger->pCounter)) SyncComputeBracketValues(pTrigger->pCounter, /*startOver*/ TRUE);}static intSyncAddTriggerToCounter(pTrigger) SyncTrigger *pTrigger;{ SyncTriggerList *pCur; if (!pTrigger->pCounter) return Success; /* don't do anything if it's already there */ for (pCur = pTrigger->pCounter->pTriglist; pCur; pCur = pCur->next) { if (pCur->pTrigger == pTrigger) return Success; } if (!(pCur = (SyncTriggerList *)xalloc(sizeof(SyncTriggerList)))) return BadAlloc; pCur->pTrigger = pTrigger; pCur->next = pTrigger->pCounter->pTriglist; pTrigger->pCounter->pTriglist = pCur; if (IsSystemCounter(pTrigger->pCounter)) SyncComputeBracketValues(pTrigger->pCounter, /*startOver*/ TRUE); return Success;}/* Below are four possible functions that can be plugged into * pTrigger->CheckTrigger, corresponding to the four possible * test-types. These functions are called after the counter's * value changes but are also passed the old counter value * so they can inspect both the old and new values. * (PositiveTransition and NegativeTransition need to see both * pieces of information.) These functions return the truth value * of the trigger. * * All of them include the condition pTrigger->pCounter == NULL. * This is because the spec says that a trigger with a counter value * of None is always TRUE. */static BoolSyncCheckTriggerPositiveComparison(pTrigger, oldval) SyncTrigger *pTrigger; CARD64 oldval;{ return (pTrigger->pCounter == NULL || XSyncValueGreaterOrEqual(pTrigger->pCounter->value, pTrigger->test_value));}static BoolSyncCheckTriggerNegativeComparison(pTrigger, oldval) SyncTrigger *pTrigger; CARD64 oldval;{ return (pTrigger->pCounter == NULL || XSyncValueLessOrEqual(pTrigger->pCounter->value, pTrigger->test_value));}static BoolSyncCheckTriggerPositiveTransition(pTrigger, oldval) SyncTrigger *pTrigger; CARD64 oldval;{ return (pTrigger->pCounter == NULL || (XSyncValueLessThan(oldval, pTrigger->test_value) && XSyncValueGreaterOrEqual(pTrigger->pCounter->value, pTrigger->test_value)));}static BoolSyncCheckTriggerNegativeTransition(pTrigger, oldval) SyncTrigger *pTrigger; CARD64 oldval;{ return (pTrigger->pCounter == NULL || (XSyncValueGreaterThan(oldval, pTrigger->test_value) && XSyncValueLessOrEqual(pTrigger->pCounter->value, pTrigger->test_value)));}static int SyncInitTrigger(client, pTrigger, counter, changes) ClientPtr client; /* so we can set errorValue */ SyncTrigger *pTrigger; XSyncCounter counter; Mask changes;{ SyncCounter *pCounter = pTrigger->pCounter; int status; Bool newcounter = FALSE; if (changes & XSyncCACounter) { if (counter == None) pCounter = NULL; else if (!(pCounter = (SyncCounter *)SecurityLookupIDByType( client, counter, RTCounter, SecurityReadAccess))) { client->errorValue = counter; return SyncErrorBase + XSyncBadCounter; } if (pCounter != pTrigger->pCounter) { /* new counter for trigger */ SyncDeleteTriggerFromCounter(pTrigger); pTrigger->pCounter = pCounter; newcounter = TRUE; } } /* if system counter, ask it what the current value is */ if (IsSystemCounter(pCounter)) { (*pCounter->pSysCounterInfo->QueryValue) ((pointer) pCounter, &pCounter->value); } if (changes & XSyncCAValueType) { if (pTrigger->value_type != XSyncRelative && pTrigger->value_type != XSyncAbsolute) { client->errorValue = pTrigger->value_type; return BadValue; } } if (changes & XSyncCATestType) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -