summaryrefslogtreecommitdiff
path: root/zip/ZipArchive/ZipMutex_lnx.h
blob: fde12b6a20378043644991b9d4e2152ab1e55d78 (plain)
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
////////////////////////////////////////////////////////////////////////////////
// This source file is part of the ZipArchive Library Open Source distribution 
// and is Copyrighted 2000 - 2022 by Artpol Software - Tadeusz Dracz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// For the licensing details refer to the License.txt file.
//
// Web Site: https://www.artpol-software.com
////////////////////////////////////////////////////////////////////////////////

#ifndef ZIPARCHIVE_ZIPMUTEX_DOT_H
	#error Do not include this file directly. Include ZipMutex.h instead
#endif

#ifdef _ZIP_USE_LOCKING

#include <pthread.h>

namespace ZipArchiveLib
{

class ZIP_API CZipMutex
{
	pthread_mutex_t m_mutex;
	bool m_bOpened;
public:
	CZipMutex(bool bOpen = false)
	{
		if (bOpen)
			Open();
		else
			m_bOpened = false;
	}

	void Open()
	{
		Close();
		if (pthread_mutex_init(&m_mutex, NULL) == 0)
			m_bOpened = true;
		else
			CZipException::Throw(CZipException::mutexError);
	}

	void Lock()
	{
		if (pthread_mutex_lock(&m_mutex) != 0)
			CZipException::Throw(CZipException::mutexError);
	}

	void Unlock()
	{
		if (pthread_mutex_unlock(&m_mutex) != 0)
			CZipException::Throw(CZipException::mutexError);
	}

	CZipMutex& operator=(const CZipMutex&)
	{
		m_bOpened = false;
		return *this;
	}


	void Close()
	{
		if (m_bOpened)
        {
			if (pthread_mutex_destroy(&m_mutex) == 0)
            {
				m_bOpened = false;
            }
			else
            {
                CZipException::Throw(CZipException::mutexError);
            }
        }
	}

	~CZipMutex()
	{		 
		Close();
	}
};

}

#endif