AD

CLucene source analysis (c) cross-platform, thread-safe

Home> CLucene, program Life> CLucene source analysis (c) cross-platform, thread-safe
CLucene source analysis (c) cross-platform, thread-safe
May 29, 2009 Xiao Wu brother comment read comments in the multi-threaded programming, the program thread-safe (thread-safe) is very important to do this, on the one hand in the programming process, to make possible to design more single-threaded access to the data structure, one that is reasonable to use the lock (Mutex). In CLucene, the realization of the following cross-platform lock-related macros:
(1) _LUCENE_SLEEP (x) suspends the current thread x microseconds
(2) _LUCENE_THREADMUTEX thread lock (mutex)
(3) _LUCENE_CURRTHREADID for the current thread's thread ID
(4) _LUCENE_THREADID_TYPE thread ID of the type defined by the above macros, you can achieve the basic thread safety related operations. The following concrete realization of the above macro to detail one by one:
1. The windows platform:
(1) _LUCENE_SLEEP (x) by the system API Sleep () implementation

1
# Define _LUCENE_SLEEP (x) Sleep (x)

(2) _LUCENE_THREADMUTEX implementation by the relevant content CRITICAL_SECTION

1
# Define _LUCENE_THREADMUTEX CL_NS (util):: mutex_win32

(3) _LUCENE_CURRTHREADID by the system API GetCurrentThreadId () implementation

1
# Define _LUCEN colla = "E_CURRTHREADID GetCurrentThreadId ()

(4) _LUCENE_THREADID_TYPE type DWORD type

1
# Define _LUCENE_THREADID_TYPE DWORD

2. In the unix platform:
(1) _LUCENE_SLEEP (x) by the system function usleep () implementation

1
# Define _LUCENE_SLEEP (x) usleep (x * 1000)

(2) _LUCENE_THREADMUTEX implementation by the relevant content pthread_mutex_t

1
# Define _LUCENE_THREADMUTEX CL_NS (util):: mutex_pthread

(3) _LUCENE_CURRTHREADID by the pthread in pthread_self () implementation

1
# Define _LUCENE_CURRTHREADID pthread_self ()

(4) _LUCENE_THREADID_TYPE type pthread_t type

1
# Define _LUCENE_THREADID_TYPE pthread_t

From the above explanation, we can see _LUCENE_THREADMUTEX implementation, or a layer of packaging, under the windows is mutex_win32, under the unix is mutex_pthread, so in order to in-depth understanding, you also need a more in-depth step.
Here is mutex_win32 implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class mutex_win32
{
private:
CRITICAL_SECTION mtx;
public:
mutex_win32 (const mutex_win32 & clone);
mutex_win32 ();
~ Mutex_win32 ();
void lock ();
void unlock ();
};
mutex_win32:: mutex_win32 (const mutex_win32 & clone) {
InitializeCriticalSection (& mtx);
}
mutex_win32:: mutex_win32 ()
{
InitializeCriticalSection (& mtx);
}

mutex_win32:: ~ mutex_win32 ()
{
DeleteCriticalSection (& mtx);
}

void mutex_win32:: lock ()
{
EnterCriticalSection (& mtx);
}

void mutex_win32:: unlock ()
{
LeaveCriticalSection (& mtx);
}

Here is mutex_pthread implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class mutex_pthread
{
private:
pthread_mutex_t mtx;

public:
mutex_pthread (const mutex_pthread & clone);
mutex_pthread ();
~ Mutex_pthread ();
void lock ();
void unlock ();

private:
# Ifndef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
pthread_t lockOwner;
unsigned int lockCount;
# Endif
};
# Ifdef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
bool mutex_pthread_attr_initd = false;
pthread_mutexattr_t mutex_pthread_attr;
# Endif

# Ifdef _CL__CND_DEBUG
# Define _CLPTHREAD_CHECK (c, m) CND_PRECONDITION (c == 0, m)
# Else
# Define _CLPTHREAD_CHECK (c, m) c;
# Endif

mutex_pthread:: mutex_pthread (const mutex_pthread & clone) {
# Ifdef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
_CLPTHREAD_CHECK (Pthread_mutex_init (& mtx, & mutex_pthread_attr), "mutex_pthread (clone) constructor failed")
# Else
# If defined (__hpux) & & defined (_DECTHREADS_)
_CLPTHREAD_CHECK (Pthread_mutex_init (& mtx, pthread_mutexattr_default), "mutex_pthread (clone) constructor failed")
# Else
_CLPTHREAD_CHECK (Pthread_mutex_init (& mtx, 0), "mutex_pthread (clone) constructor failed")
# Endif
lockCount = 0;
lockOwner = 0;
# Endif
}
mutex_pthread:: mutex_pthread ()
{
# Ifdef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
if (mutex_pthread_attr_initd == false) {
pthread_mutexattr_init (& mutex_pthread_attr);
pthread_mutexattr_settype (& mutex_pthread_attr, PTHREAD_MUTEX_RECURSIVE);
mutex_pthread_attr_initd = true;
}
_CLPTHREAD_CHECK (Pthread_mutex_init (& mtx, & mutex_pthread_attr), "mutex_pthread (clone) constructor failed")
# Else
# If defined (__hpux) & & defined (_DECTHREADS_)
_CLPTHREAD_CHECK (Pthread_mutex_init (& mtx, pthread_mutexattr_default), "mutex_pthread (clone) constructor failed")
# Else
_CLPTHREAD_CHECK (Pthread_mutex_init (& mtx, 0), "mutex_pthread (clone) constructor failed")
# Endif
lockCount = 0;
lockOwner = 0;
# Endif
}

mutex_pthread:: ~ mutex_pthread ()
{
_CLPTHREAD_CHECK (Pthread_mutex_destroy (& mtx), "~ mutex_pthread destructor failed")
}

void mutex_pthread:: lock ()
{
# Ifndef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
pthread_t currentThread = pthread_self ();
if (pthread_equal (lockOwner, currentThread)) {
+ + LockCount;
} Else {
_CLPTHREAD_CHECK (Pthread_mutex_lock (& mtx), "mutex_pthread:: lock")
lockOwner = currentThread;
lockCount = 1;
}
# Else
_CLPTHREAD_CHECK (Pthread_mutex_lock (& mtx), "mutex_pthread:: lock")
# Endif
}

void mutex_pthread:: unlock ()
{
# Ifndef _CL_HAVE_PTHREAD_MUTEX_RECURSIVE
- LockCount;
if (lockCount == 0)
{
lockOwner = 0;
_CLPTHREAD_CHECK (Pthread_mutex_unlock (& mtx), "mutex_pthread:: unlock")
}
# Else
_CLPTHREAD_CHECK (Pthread_mutex_unlock (& mtx), "mutex_pthread:: unlock")
# Endif
}

Implementation can be seen from the above, mutex_win32 CRITICAL_SECTION directly related to the operation is carried out on the packaging, and related operations mutex_pthread is pthread_mutex_t package. In fact, here, I do not know why using this macro on the way, I think the package can be had a common Thread class and Mutex class, so it is more convenient to use and easier to do extensions.
****wuzesheng**/?p=120
标签: windows platform, cross platform, data structure, lucene, thread id, realization, system api, thread safety, microseconds, source analysis, platform 1, xiao wu, unix platform, program thread, mutex, critical section, c cross, section 1, safe home, self implementation
分类: CPP
时间: 2010-12-21

相关文章

  1. SQLITE Source Analysis (8)

    Disclaimer: The SQLite source code analysis series Xing (http://deepfuture.javaeye.com/) original, without aut ...
  2. TOMCAT Source Analysis (start frame)

    Reprinted: http://edu.codepub.com/2010/0518/22773.php TOMCAT Source Analysis (start frame) Introduction: This ...
  3. struts2 in action is thread safe, struts1 the action is not thread safe

    Struts2 in action Why is thread safe, struts1 the action is not thread safe? Struts2 first principle of struts ...
  4. PHP version of VC6 and VC9.Non Thread Safe and Thread Safe differences

    Want to update a PHP version, PHP version of the windows have been separated out, see http://windows.php.net/d ...
  5. Struts1 Source analysis - international resource file

    Struts1 Source analysis - international resource file 1. URL class diagram 2. Description a) Struts resources ...
  6. PHP thread safe and non thread safe difference between

    Windows version of PHP version 5.2.1 from the beginning to Thread Safe (thread safe), and None Thread Safe (NT ...
  7. HashMap source analysis

    HashMap source analysis public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V&g ...
  8. Lucene3.0 Source Analysis (1) in the Eclipse / MyEclipse project to build Lucene3.0

    Source analysis of the first step is to establish related projects in the IDE, and then go step by step learni ...
  9. PHP on VC9 and VC6 and Thread Safe and Non Thread Safe version of choice

    PHP official website recently to see the new version of PHP to download again, and then up Zhaozhao For Window ...
  10. Axis source analysis - the server processes the request and response (3)

    Source analysis of the Axis-Web Services Deployment (II) http://dead-knight.javaeye.com/blog/732961 Has two we ...
  11. PHP5 in Thread Safe (thread safe) None Thread Safe (NTS, non-thread safe) the difference between

    indows version of PHP from version 5.2.1 started Thread Safe (thread safe), and None Thread Safe (NTS, non-thr ...
  12. reentrant function is thread safe function

    Access to the course of the investigation reentrant functions - reentrant function is too long, and not at all ...
  13. Why, compared with the StringBuffer StringBuilder is not thread safe

    Examples of thread unsafe StringBuilder package com.xiva.demo; public class StringBuilderDemo extends Thread { ...
  14. 解析PHP中VC6 X86和VC9 X86的区别及 Non Thread Safe的意思

    本篇文章是对PHP中VC6 X86和VC9 X86的区别及 Non Thread Safe的意思进行了详细的分析介绍,需要的朋友参考下 PHP现在推出5.3.0版本了,不过下载的时候有几个不同版本选择.那就是VC6 X8 ...
  15. PHP 5.3 下载时 VC9.VC6.Thread Safe.Non Thread Safe的区别分析

    我最近在 PHP 官网上看到又有新版的 PHP 下载了,于是上去找找 For Windows 的版本,可是一看确傻眼了 一共给了四个版本,VC9 x86 Non Thread Safe.VC9 x86 Thread Sa ...
  16. PHP关于VC9和VC6以及Thread Safe和Non Thread Safe版本选择

    一.如何选择PHP5.3的VC9版本和VC6版本 VC6版本是使用Visual Studio 6编译器编译的,如果你的PHP是用Apache来架设的,那你就选择VC6版本. VC9版本是使用Visual Studio 2 ...
  17. PHP版本VC6与VC9.Thread Safe与None-Thread Safe等的区别

    转载一篇 最近发现很多PHP程序员对PHP版本知识了解不是很清楚,自己也看了不少类似的文章,还是感觉不够明确和全面,网上的结论又都是模棱两可,在此,给出最完整甚至武断的解释. 本文讲解:VC6与VC9,Thread Sa ...
  18. PHP版本VC6与VC9.Thread Safe与None-Thread Safe

    如果你在apache1或者apache2下使用PHP,你应该选择VC6的版本 如果你在IIS下使用PHP应该选择VC9的版本 VC6的版本使用visual studio6编译 VC9使用Visual Studio 200 ...
  19. Android4 Source Analysis - Root-External

    A-D E-G H-K L-N O-Q R-T U-W X-Z A-D android-mock/ - http://code.google.com/p/android-mock/ - Notes Android Moc ...