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

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? 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;

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美一二三区在线观看| 国产精品综合一区二区三区| 久久久综合精品| 国产日韩欧美不卡| 精品久久久久久最新网址| 日韩一卡二卡三卡| 欧美疯狂做受xxxx富婆| 欧美三级电影一区| 欧美一区二区三区日韩| 国产精品情趣视频| 亚洲电影一区二区三区| 日韩av在线发布| 91黄色激情网站| 精品999久久久| 丝袜亚洲另类丝袜在线| 91啪亚洲精品| 国产精品888| 92国产精品观看| 国产亚洲欧美中文| 精品在线你懂的| 538在线一区二区精品国产| 中文字幕一区二区三区av| 国产精品影视在线| 91精选在线观看| 天天操天天干天天综合网| 色欧美乱欧美15图片| 国产精品久久久久久久久果冻传媒 | 欧美日韩精品电影| 天堂影院一区二区| 91麻豆精品国产自产在线| 丝袜诱惑制服诱惑色一区在线观看 | 精品久久久久久久久久久久包黑料 | 国产精品18久久久久久vr| 精品久久五月天| 国产精品一二三区| 欧美激情一区在线| 色老汉一区二区三区| 亚洲精品乱码久久久久久| 色综合一个色综合亚洲| 中文字幕的久久| 欧美揉bbbbb揉bbbbb| 视频一区二区三区中文字幕| 日韩一二三四区| 成人app在线观看| 日本麻豆一区二区三区视频| 欧美国产激情二区三区| 色综合久久综合网欧美综合网 | 在线免费观看一区| 蜜臂av日日欢夜夜爽一区| 国产精品美女久久久久久久久久久 | 久久久99精品免费观看不卡| 欧美吞精做爰啪啪高潮| 亚洲图片欧美综合| 日韩精品影音先锋| 国产成人高清在线| 亚洲欧洲美洲综合色网| 欧美在线影院一区二区| 美女视频黄久久| 中文字幕色av一区二区三区| 欧美日韩视频在线第一区| 国产在线麻豆精品观看| 亚洲国产毛片aaaaa无费看 | 色综合久久久久久久久| 国产乱码精品一区二区三区忘忧草| 一区二区三区色| 亚洲少妇30p| 国产精品欧美极品| 国产午夜一区二区三区| 精品盗摄一区二区三区| 日韩一区二区三区三四区视频在线观看| 99精品视频在线免费观看| 成人av先锋影音| 日本久久电影网| 欧美日韩一区二区在线视频| 日韩一区二区三区观看| 中文文精品字幕一区二区| 亚洲欧美另类小说| 91精品免费在线观看| 久久婷婷一区二区三区| 91麻豆精品国产91久久久久久久久| 久久99国产精品麻豆| 一区二区三区中文字幕电影 | kk眼镜猥琐国模调教系列一区二区 | 天天免费综合色| 日韩经典一区二区| 日本亚洲一区二区| 五月激情六月综合| 亚洲国产综合视频在线观看| 国产精品天干天干在观线| 欧美三级视频在线| 一本色道久久综合亚洲aⅴ蜜桃 | 国产精品一线二线三线精华| 日韩午夜激情av| 97久久人人超碰| jlzzjlzz欧美大全| 99re热这里只有精品视频| 久久国产夜色精品鲁鲁99| 亚洲一线二线三线久久久| 久久久.com| 宅男噜噜噜66一区二区66| 91超碰这里只有精品国产| av电影在线观看不卡| 成人免费看视频| 黄色日韩三级电影| 五月天国产精品| 五月婷婷综合激情| 99久久精品情趣| 18欧美亚洲精品| 色婷婷久久一区二区三区麻豆| **性色生活片久久毛片| 92国产精品观看| 亚洲黄色小视频| 久久综合一区二区| av不卡一区二区三区| 9191成人精品久久| 日韩精品在线看片z| 国产精品三级视频| 日韩影院在线观看| 国产suv精品一区二区883| 国产盗摄一区二区| 色老汉一区二区三区| 久久久久久久久久久久久久久99 | 国产乱子伦一区二区三区国色天香| 91麻豆产精品久久久久久| 亚洲欧洲日韩av| 色综合天天综合色综合av| 一区免费观看视频| 欧洲一区二区三区免费视频| 国产精品盗摄一区二区三区| 成人aa视频在线观看| 亚洲精品美国一| 3atv一区二区三区| 久99久精品视频免费观看| 久久久精品免费网站| 色综合久久中文综合久久97| 国产精品亚洲视频| 色噜噜狠狠一区二区三区果冻| 日韩欧美精品在线视频| 久久精品无码一区二区三区| 亚洲精品菠萝久久久久久久| 图片区日韩欧美亚洲| 丰满少妇久久久久久久| 久久伊人蜜桃av一区二区| 国内精品久久久久影院色 | 欧美不卡一二三| 国产欧美一区二区在线观看| 洋洋成人永久网站入口| 国产传媒欧美日韩成人| 26uuu另类欧美| 91精品办公室少妇高潮对白| 亚洲欧洲综合另类| 国产激情91久久精品导航| 亚洲精品国产a| 欧美成人精品1314www| 91女人视频在线观看| 奇米精品一区二区三区在线观看一| 国产精品丝袜久久久久久app| 欧美日韩免费在线视频| 成人av网站在线观看免费| 日韩电影在线观看电影| 一区二区三区91| 东方欧美亚洲色图在线| 午夜视频在线观看一区| 日韩天堂在线观看| 欧美在线观看一区二区| 91色乱码一区二区三区| 成人午夜视频网站| 国内精品视频一区二区三区八戒| 天天影视涩香欲综合网 | 欧洲人成人精品| 97se亚洲国产综合自在线不卡| 国产大片一区二区| 国产美女精品一区二区三区| 九九九久久久精品| 国产精品资源站在线| 国产剧情一区二区三区| 丁香另类激情小说| 色综合久久中文综合久久97| 欧洲国产伦久久久久久久| 欧美日韩一区久久| 欧美二区乱c少妇| 久久久久久99精品| 亚洲欧美中日韩| 午夜精品影院在线观看| 久久精品国产第一区二区三区| 国产精品一二二区| 在线看日韩精品电影| 国产成人av一区二区| 91看片淫黄大片一级| 欧美美女网站色| 国产午夜精品一区二区三区四区| 成人欧美一区二区三区小说| 丝袜国产日韩另类美女| 国产精品69久久久久水密桃| 欧美中文字幕亚洲一区二区va在线 | 国产欧美精品一区| 丝袜美腿成人在线| 9色porny自拍视频一区二区| 久久夜色精品国产噜噜av| 久久久美女毛片|