亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

?? nt.cpp

?? tightvnc 1.3.9 的源代碼
?? CPP
?? 第 1 頁(yè) / 共 2 頁(yè)
字號(hào):
//				Package : omnithread
// omnithread/nt.cc		Created : 6/95 tjr
//
//    Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
//
//    This file is part of the omnithread library
//
//    The omnithread library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Library General Public
//    License as published by the Free Software Foundation; either
//    version 2 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Library General Public License for more details.
//
//    You should have received a copy of the GNU Library General Public
//    License along with this library; if not, write to the Free
//    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
//    02111-1307, USA
//

//
// Implementation of OMNI thread abstraction for NT threads
//

#include <stdlib.h>
#include <errno.h>
#include "omnithread.h"
#include <process.h>

#define DB(x) // x 
//#include <iostream.h> or #include <iostream> if DB is on.

static void get_time_now(unsigned long* abs_sec, unsigned long* abs_nsec);

///////////////////////////////////////////////////////////////////////////
//
// Mutex
//
///////////////////////////////////////////////////////////////////////////


omni_mutex::omni_mutex(void)
{
    InitializeCriticalSection(&crit);
}

omni_mutex::~omni_mutex(void)
{
    DeleteCriticalSection(&crit);
}

void
omni_mutex::lock(void)
{
    EnterCriticalSection(&crit);
}

void
omni_mutex::unlock(void)
{
    LeaveCriticalSection(&crit);
}



///////////////////////////////////////////////////////////////////////////
//
// Condition variable
//
///////////////////////////////////////////////////////////////////////////


//
// Condition variables are tricky to implement using NT synchronisation
// primitives, since none of them have the atomic "release mutex and wait to be
// signalled" which is central to the idea of a condition variable.  To get
// around this the solution is to record which threads are waiting and
// explicitly wake up those threads.
//
// Here we implement a condition variable using a list of waiting threads
// (protected by a critical section), and a per-thread semaphore (which
// actually only needs to be a binary semaphore).
//
// To wait on the cv, a thread puts itself on the list of waiting threads for
// that cv, then releases the mutex and waits on its own personal semaphore.  A
// signalling thread simply takes a thread from the head of the list and kicks
// that thread's semaphore.  Broadcast is simply implemented by kicking the
// semaphore of each waiting thread.
//
// The only other tricky part comes when a thread gets a timeout from a timed
// wait on its semaphore.  Between returning with a timeout from the wait and
// entering the critical section, a signalling thread could get in, kick the
// waiting thread's semaphore and remove it from the list.  If this happens,
// the waiting thread's semaphore is now out of step so it needs resetting, and
// the thread should indicate that it was signalled rather than that it timed
// out.
//
// It is possible that the thread calling wait or timedwait is not a
// omni_thread. In this case we have to provide a temporary data structure,
// i.e. for the duration of the call, for the thread to link itself on the
// list of waiting threads. _internal_omni_thread_dummy provides such
// a data structure and _internal_omni_thread_helper is a helper class to
// deal with this special case for wait() and timedwait(). Once created,
// the _internal_omni_thread_dummy is cached for use by the next wait() or
// timedwait() call from a non-omni_thread. This is probably worth doing
// because creating a Semaphore is quite heavy weight.

class _internal_omni_thread_helper;

class _internal_omni_thread_dummy : public omni_thread {
public:
  inline _internal_omni_thread_dummy() : next(0) { }
  inline ~_internal_omni_thread_dummy() { }
  friend class _internal_omni_thread_helper;
private:
  _internal_omni_thread_dummy* next;
};

class _internal_omni_thread_helper {
public:
  inline _internal_omni_thread_helper()  { 
    d = 0;
    t = omni_thread::self();
    if (!t) {
      omni_mutex_lock sync(cachelock);
      if (cache) {
	d = cache;
	cache = cache->next;
      }
      else {
	d = new _internal_omni_thread_dummy;
      }
      t = d;
    }
  }
  inline ~_internal_omni_thread_helper() { 
    if (d) {
      omni_mutex_lock sync(cachelock);
      d->next = cache;
      cache = d;
    }
  }
  inline operator omni_thread* () { return t; }
  inline omni_thread* operator->() { return t; }

  static _internal_omni_thread_dummy* cache;
  static omni_mutex                   cachelock;

private:
  _internal_omni_thread_dummy* d;
  omni_thread*                 t;
};

_internal_omni_thread_dummy* _internal_omni_thread_helper::cache = 0;
omni_mutex                   _internal_omni_thread_helper::cachelock;


omni_condition::omni_condition(omni_mutex* m) : mutex(m)
{
    InitializeCriticalSection(&crit);
    waiting_head = waiting_tail = NULL;
}


omni_condition::~omni_condition(void)
{
    DeleteCriticalSection(&crit);
    DB( if (waiting_head != NULL) {
	cerr << "omni_condition::~omni_condition: list of waiting threads "
	     << "is not empty\n";
    } )
}


void
omni_condition::wait(void)
{
    _internal_omni_thread_helper me;

    EnterCriticalSection(&crit);

    me->cond_next = NULL;
    me->cond_prev = waiting_tail;
    if (waiting_head == NULL)
	waiting_head = me;
    else
	waiting_tail->cond_next = me;
    waiting_tail = me;
    me->cond_waiting = TRUE;

    LeaveCriticalSection(&crit);

    mutex->unlock();

    DWORD result = WaitForSingleObject(me->cond_semaphore, INFINITE);

    mutex->lock();

    if (result != WAIT_OBJECT_0)
	throw omni_thread_fatal(GetLastError());
}


int
omni_condition::timedwait(unsigned long abs_sec, unsigned long abs_nsec)
{
    _internal_omni_thread_helper me;

    EnterCriticalSection(&crit);

    me->cond_next = NULL;
    me->cond_prev = waiting_tail;
    if (waiting_head == NULL)
	waiting_head = me;
    else
	waiting_tail->cond_next = me;
    waiting_tail = me;
    me->cond_waiting = TRUE;

    LeaveCriticalSection(&crit);

    mutex->unlock();

    unsigned long now_sec, now_nsec;

    get_time_now(&now_sec, &now_nsec);

    DWORD timeout = (abs_sec-now_sec) * 1000 + (abs_nsec-now_nsec) / 1000000;

    if ((abs_sec <= now_sec) && ((abs_sec < now_sec) || (abs_nsec < abs_nsec)))
	timeout = 0;

    DWORD result = WaitForSingleObject(me->cond_semaphore, timeout);

    if (result == WAIT_TIMEOUT) {
	EnterCriticalSection(&crit);

	if (me->cond_waiting) {
	    if (me->cond_prev != NULL)
		me->cond_prev->cond_next = me->cond_next;
	    else
		waiting_head = me->cond_next;
	    if (me->cond_next != NULL)
		me->cond_next->cond_prev = me->cond_prev;
	    else
		waiting_tail = me->cond_prev;
	    me->cond_waiting = FALSE;

	    LeaveCriticalSection(&crit);

	    mutex->lock();
	    return 0;
	}

	//
	// We timed out but another thread still signalled us.  Wait for
	// the semaphore (it _must_ have been signalled) to decrement it
	// again.  Return that we were signalled, not that we timed out.
	//

	LeaveCriticalSection(&crit);

	result = WaitForSingleObject(me->cond_semaphore, INFINITE);
    }

    if (result != WAIT_OBJECT_0)
	throw omni_thread_fatal(GetLastError());

    mutex->lock();
    return 1;
}


void
omni_condition::signal(void)
{
    EnterCriticalSection(&crit);

    if (waiting_head != NULL) {
	omni_thread* t = waiting_head;
	waiting_head = t->cond_next;
	if (waiting_head == NULL)
	    waiting_tail = NULL;
	else
	    waiting_head->cond_prev = NULL;
	t->cond_waiting = FALSE;

	if (!ReleaseSemaphore(t->cond_semaphore, 1, NULL)) {
	    int rc = GetLastError();
	    LeaveCriticalSection(&crit);
	    throw omni_thread_fatal(rc);
	}
    }

    LeaveCriticalSection(&crit);
}


void
omni_condition::broadcast(void)
{
    EnterCriticalSection(&crit);

    while (waiting_head != NULL) {
	omni_thread* t = waiting_head;
	waiting_head = t->cond_next;
	if (waiting_head == NULL)
	    waiting_tail = NULL;
	else
	    waiting_head->cond_prev = NULL;
	t->cond_waiting = FALSE;

	if (!ReleaseSemaphore(t->cond_semaphore, 1, NULL)) {
	    int rc = GetLastError();
	    LeaveCriticalSection(&crit);
	    throw omni_thread_fatal(rc);
	}
    }

    LeaveCriticalSection(&crit);
}



///////////////////////////////////////////////////////////////////////////
//
// Counting semaphore
//
///////////////////////////////////////////////////////////////////////////


#define SEMAPHORE_MAX 0x7fffffff


omni_semaphore::omni_semaphore(unsigned int initial)
{
    nt_sem = CreateSemaphore(NULL, initial, SEMAPHORE_MAX, NULL);

    if (nt_sem == NULL) {
      DB( cerr << "omni_semaphore::omni_semaphore: CreateSemaphore error "
	     << GetLastError() << endl );
      throw omni_thread_fatal(GetLastError());
    }
}


omni_semaphore::~omni_semaphore(void)
{
  if (!CloseHandle(nt_sem)) {
    DB( cerr << "omni_semaphore::~omni_semaphore: CloseHandle error "
	     << GetLastError() << endl );
    throw omni_thread_fatal(GetLastError());
  }
}


void
omni_semaphore::wait(void)
{
    if (WaitForSingleObject(nt_sem, INFINITE) != WAIT_OBJECT_0)
	throw omni_thread_fatal(GetLastError());
}


int
omni_semaphore::trywait(void)
{
    switch (WaitForSingleObject(nt_sem, 0)) {

    case WAIT_OBJECT_0:
	return 1;
    case WAIT_TIMEOUT:
	return 0;
    }

    throw omni_thread_fatal(GetLastError());
    return 0; /* keep msvc++ happy */
}


void
omni_semaphore::post(void)
{
    if (!ReleaseSemaphore(nt_sem, 1, NULL))
	throw omni_thread_fatal(GetLastError());
}



///////////////////////////////////////////////////////////////////////////
//
// Thread
//
///////////////////////////////////////////////////////////////////////////


//
// Static variables
//

int omni_thread::init_t::count = 0;

omni_mutex* omni_thread::next_id_mutex;
int omni_thread::next_id = 0;
static DWORD self_tls_index;

//
// Initialisation function (gets called before any user code).
//

omni_thread::init_t::init_t(void)
{
    if (count++ != 0)	// only do it once however many objects get created.
	return;

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
在线视频一区二区三| 精品美女一区二区三区| 另类中文字幕网| 亚洲人妖av一区二区| 日韩免费在线观看| 日本韩国一区二区| 国产99久久久久| 免费的国产精品| 亚洲bdsm女犯bdsm网站| 成人欧美一区二区三区在线播放| 欧美电影免费提供在线观看| 欧美日韩精品一区二区三区 | 国产欧美在线观看一区| 欧美精品久久久久久久多人混战| 91视频国产资源| 国产成人精品一区二区三区网站观看| 视频一区视频二区中文字幕| 亚洲精品少妇30p| 中文字幕亚洲欧美在线不卡| 久久女同性恋中文字幕| 日韩视频在线观看一区二区| 欧美亚洲动漫精品| 色综合 综合色| www.欧美.com| 波多野结衣亚洲一区| 国模一区二区三区白浆| 久久99热这里只有精品| 日本不卡一区二区三区高清视频| 亚洲免费观看高清完整版在线观看| 欧美激情综合五月色丁香小说| 欧美白人最猛性xxxxx69交| 91精品蜜臀在线一区尤物| 欧美曰成人黄网| 91福利在线导航| 色哟哟在线观看一区二区三区| 99re这里只有精品视频首页| av电影在线观看完整版一区二区| 成人av中文字幕| 99精品视频在线观看免费| 99国产精品国产精品久久| jlzzjlzz亚洲女人18| 99久久精品国产观看| av在线不卡网| 色吧成人激情小说| 欧美午夜影院一区| 欧美精品三级在线观看| 日韩一级高清毛片| 日韩精品一区二区三区视频| 精品99999| 国产日韩欧美精品综合| 成人欧美一区二区三区小说 | 亚洲人成小说网站色在线| 亚洲精品国产a| 性欧美大战久久久久久久久| 免费人成在线不卡| 精品系列免费在线观看| 成人午夜碰碰视频| 在线观看视频一区| 91麻豆精品国产91久久久久久久久| 日韩一级黄色片| 国产欧美日韩综合精品一区二区| 国产精品沙发午睡系列990531| 亚洲色图视频网| 亚欧色一区w666天堂| 美女国产一区二区| www.在线欧美| 欧美一区二区美女| 国产三级三级三级精品8ⅰ区| 中文字幕一区二区日韩精品绯色| 亚洲高清免费视频| 国产乱码精品一品二品| 91蝌蚪porny| 日韩欧美高清在线| 亚洲欧洲www| 日韩电影在线一区二区| 韩国v欧美v日本v亚洲v| 日本韩国欧美一区| 久久久亚洲精品石原莉奈| 亚洲欧美色一区| 激情综合一区二区三区| 91美女视频网站| 欧美精品一区视频| 一区二区三区国产| 国产一区不卡视频| 欧美日韩一区二区在线观看视频 | 日韩1区2区日韩1区2区| 成人黄色777网| 日韩三区在线观看| 亚洲另类在线一区| 国产综合色在线视频区| 日本丶国产丶欧美色综合| 2024国产精品| 日日摸夜夜添夜夜添精品视频| 成人黄色在线看| 日韩欧美精品在线| 亚洲午夜一二三区视频| 成人午夜激情影院| 欧美mv和日韩mv的网站| 亚洲国产综合91精品麻豆| 高清成人免费视频| 欧美成人女星排行榜| 亚洲国产成人av好男人在线观看| 国产成人精品三级| 日韩欧美中文一区| 亚洲一区二区三区爽爽爽爽爽| 成人午夜在线播放| 久久一区二区三区四区| 首页综合国产亚洲丝袜| 91福利精品第一导航| 国产精品理论片| 国内精品免费在线观看| 欧美日本韩国一区二区三区视频| 亚洲欧美色图小说| 成人国产精品视频| 国产女人18水真多18精品一级做| 蜜臀av性久久久久蜜臀aⅴ流畅 | 三级亚洲高清视频| 在线精品观看国产| 亚洲视频每日更新| a美女胸又www黄视频久久| 国产片一区二区三区| 国产一区二区三区久久久| 91精选在线观看| 午夜精品福利一区二区三区av| 97精品久久久午夜一区二区三区| 亚洲国产精华液网站w| 国产在线播放一区| 久久久久亚洲蜜桃| 国产一区二区0| 国产清纯在线一区二区www| 国产一区二区不卡| 久久久久99精品国产片| 国产乱码精品一区二区三区av | 欧美日韩精品免费观看视频| 亚洲尤物在线视频观看| 在线观看日韩精品| 亚洲自拍偷拍图区| 欧美伦理电影网| 日韩高清不卡在线| 精品欧美一区二区在线观看| 黄色日韩三级电影| 国产亚洲欧美日韩俺去了| 成人午夜免费视频| 亚洲精品免费电影| 精品视频在线免费观看| 日日噜噜夜夜狠狠视频欧美人| 4438x成人网最大色成网站| 免费在线看成人av| 久久久午夜精品| 播五月开心婷婷综合| 亚洲婷婷综合色高清在线| 色婷婷激情一区二区三区| 亚洲bt欧美bt精品| 久久综合九色综合久久久精品综合 | 欧美影院一区二区| 日本v片在线高清不卡在线观看| 欧美xxx久久| www.日韩在线| 丝袜a∨在线一区二区三区不卡| 日韩欧美高清一区| 菠萝蜜视频在线观看一区| 亚洲一区二三区| 精品免费视频一区二区| 国产69精品久久99不卡| 亚洲精品一二三区| 日韩一二三区视频| 成人手机在线视频| 亚洲成人激情综合网| 日韩美女一区二区三区| 成人午夜视频在线| 日本午夜一本久久久综合| 国产亚洲成av人在线观看导航 | 久草中文综合在线| 国产精品国产三级国产有无不卡| 欧美日韩午夜精品| 粉嫩欧美一区二区三区高清影视| 亚洲五月六月丁香激情| 久久综合成人精品亚洲另类欧美| 色诱亚洲精品久久久久久| 老司机午夜精品| 亚洲免费观看高清完整版在线观看 | 国产综合久久久久影院| 日韩一区日韩二区| 日韩欧美国产精品| 91色porny在线视频| 奇米777欧美一区二区| 午夜激情久久久| 久久综合色之久久综合| 日本丶国产丶欧美色综合| 国产一区二区在线免费观看| 亚洲欧美日韩中文播放| 久久久久久久久久美女| 欧美猛男超大videosgay| www.久久精品| 国模冰冰炮一区二区| 一卡二卡欧美日韩| 日本一区二区久久| 精品女同一区二区| 欧美日韩成人综合天天影院| 9人人澡人人爽人人精品|