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

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

?? nt.cpp

?? tightvnc源碼
?? CPP
?? 第 1 頁 / 共 2 頁
字號:
//				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;

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
色欧美日韩亚洲| 国产成人av一区二区三区在线| 91免费小视频| 中文字幕一区二区三区精华液 | 亚洲一级不卡视频| 欧美日韩视频一区二区| 午夜精品久久久久久| 在线一区二区三区四区五区| 亚洲大尺度视频在线观看| 7777女厕盗摄久久久| 蜜臀精品一区二区三区在线观看| 日韩午夜在线观看视频| 国产suv精品一区二区883| 1区2区3区欧美| 欧美色综合天天久久综合精品| 日韩和欧美的一区| 国产三级一区二区三区| 色综合久久中文综合久久牛| 午夜电影网一区| 久久久久青草大香线综合精品| 色综合天天狠狠| 日本中文字幕一区二区有限公司| 久久综合久久综合久久| 一本色道久久综合狠狠躁的推荐| 日韩激情视频网站| 国产人久久人人人人爽| 精品视频在线看| 国产成人在线视频网站| 亚洲韩国精品一区| 日本一区二区不卡视频| 7777精品久久久大香线蕉| 丁香一区二区三区| 蜜臀精品一区二区三区在线观看| 中文字幕亚洲精品在线观看| 欧美一级二级三级乱码| 91麻豆国产精品久久| 久久国产尿小便嘘嘘| 亚洲免费毛片网站| 久久久久国产精品人| 91国在线观看| 国产99久久久久| 天堂va蜜桃一区二区三区漫画版| 国产精品久久久久影院色老大| 欧美精品日韩综合在线| 99精品在线免费| 国产一区二区三区免费| 亚洲激情图片一区| 国产丝袜欧美中文另类| 欧美一卡二卡在线| 91久久线看在观草草青青| 国产成人亚洲综合a∨婷婷图片| 日韩电影一区二区三区四区| 亚洲免费伊人电影| 国产精品白丝在线| 国产免费久久精品| 26uuu亚洲婷婷狠狠天堂| 欧美老年两性高潮| 91国产丝袜在线播放| www.在线成人| 国产91丝袜在线播放0| 韩国三级电影一区二区| 日本色综合中文字幕| 亚洲成av人影院| 亚洲免费在线看| 亚洲精品视频一区| 国产精品久久久久久久久果冻传媒 | 久久久亚洲精品石原莉奈| 5月丁香婷婷综合| 欧美日韩精品一区二区三区四区| 91伊人久久大香线蕉| 粉嫩一区二区三区性色av| 国产一区美女在线| 国产麻豆精品在线| 激情深爱一区二区| 韩国一区二区视频| 国产99久久久国产精品| 国产福利一区在线| 成人一区二区在线观看| 丁香亚洲综合激情啪啪综合| 成人手机电影网| 成人av网站大全| 91免费小视频| 欧美天堂亚洲电影院在线播放| 色老综合老女人久久久| 欧美自拍丝袜亚洲| 欧美精品丝袜中出| 日韩午夜av电影| www国产亚洲精品久久麻豆| 精品成人免费观看| 亚洲国产精品传媒在线观看| 中文字幕一区二区三区蜜月 | 亚洲第一狼人社区| 丝袜亚洲另类丝袜在线| 青青草国产精品97视觉盛宴| 久久精品国产网站| 国产成人午夜高潮毛片| 91在线视频官网| 欧美日韩和欧美的一区二区| 91精品国模一区二区三区| 亚洲精品在线免费观看视频| 欧美国产精品中文字幕| 亚洲另类在线一区| 欧美bbbbb| av中文一区二区三区| 精品视频免费看| 精品久久久久久亚洲综合网| 国产精品国产精品国产专区不蜜| 亚洲乱码国产乱码精品精98午夜 | 久久精品久久综合| 国产不卡免费视频| 色猫猫国产区一区二在线视频| 欧美日韩小视频| 久久精品日韩一区二区三区| 亚洲欧美另类小说| 久久精品免费观看| 色94色欧美sute亚洲线路二 | 高清国产午夜精品久久久久久| 一本一本久久a久久精品综合麻豆| 欧美一卡2卡三卡4卡5免费| 国产偷国产偷亚洲高清人白洁| 亚洲一线二线三线久久久| 国内精品嫩模私拍在线| 欧美无砖砖区免费| 国产亚洲精品7777| 日韩电影在线观看网站| 99久久久久免费精品国产 | 国产一区二区三区四区五区入口 | 国产精品国产精品国产专区不蜜| 亚洲电影在线播放| 丰满少妇在线播放bd日韩电影| 欧美高清视频不卡网| 国产精品妹子av| 国产原创一区二区三区| 欧美日韩亚洲综合一区| 18欧美乱大交hd1984| 韩国视频一区二区| 欧美一区二区三区喷汁尤物| 亚洲欧美欧美一区二区三区| 国内精品写真在线观看| 欧美一区二区三区视频免费| 一区二区三区中文字幕| 风间由美一区二区三区在线观看 | 国产成人精品影院| 日韩精品一区二区三区老鸭窝| 亚洲一二三四区不卡| 成人白浆超碰人人人人| 欧美不卡一区二区| 日韩avvvv在线播放| 欧美午夜宅男影院| 一区二区三区产品免费精品久久75| 国产精品123| 精品成人免费观看| 极品瑜伽女神91| 日韩欧美在线不卡| 天天操天天干天天综合网| 在线视频欧美精品| 亚洲综合一区在线| 91免费看视频| 亚洲国产精品国自产拍av| 国产精品一二二区| 国产午夜亚洲精品羞羞网站| 国内成人精品2018免费看| 日韩精品中午字幕| 久久99精品久久久久| 精品国产网站在线观看| 另类调教123区| 欧美mv和日韩mv的网站| 蜜臀av性久久久久蜜臀aⅴ流畅 | 精品理论电影在线观看| 美女视频一区二区三区| 精品国产污网站| 国产精品中文字幕日韩精品 | av不卡一区二区三区| 国产精品美女视频| 一本久道久久综合中文字幕| 亚洲精品乱码久久久久| 91久久国产综合久久| 午夜欧美在线一二页| 欧美一二三区在线观看| 久久91精品国产91久久小草| 久久久久久免费毛片精品| 福利电影一区二区| 一区二区三区产品免费精品久久75| 在线观看91视频| 美女在线观看视频一区二区| 久久婷婷色综合| av电影在线观看不卡| 一二三四社区欧美黄| 欧美精品一卡二卡| 激情伊人五月天久久综合| 国产女主播一区| 欧美三区在线观看| 美女脱光内衣内裤视频久久网站| 久久久美女艺术照精彩视频福利播放| 国产91丝袜在线播放| 夜夜精品视频一区二区| 欧美成人一区二区三区| 丁香婷婷综合五月| 午夜精品成人在线视频| 2023国产精品自拍|