]> git.cworth.org Git - apitrace/blob - os_posix.cpp
Accept zero valued bitmasks.
[apitrace] / os_posix.cpp
1 /**************************************************************************
2  *
3  * Copyright 2010 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29
30 #include <unistd.h>
31 #include <sys/time.h>
32 #include <pthread.h>
33
34 #include "os.hpp"
35
36 namespace OS {
37
38
39 static pthread_mutex_t 
40 mutex = PTHREAD_MUTEX_INITIALIZER;
41
42
43 void
44 AcquireMutex(void)
45 {
46     pthread_mutex_lock(&mutex);
47 }
48
49
50 void
51 ReleaseMutex(void)
52 {
53     pthread_mutex_unlock(&mutex);
54 }
55
56
57 bool
58 GetProcessName(char *str, size_t size)
59 {
60     ssize_t len;
61     char szProcessPath[PATH_MAX + 1];
62     char *lpProcessName;
63
64     // http://stackoverflow.com/questions/1023306/finding-current-executables-path-without-proc-self-exe
65     len = readlink("/proc/self/exe", szProcessPath, sizeof(szProcessPath) - 1);
66     if (len == -1) {
67         *str = 0;
68         return false;
69     }
70     szProcessPath[len] = 0;
71
72     lpProcessName = strrchr(szProcessPath, '/');
73     lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
74
75     strncpy(str, lpProcessName, size);
76     if (size)
77         str[size - 1] = 0;
78
79     return true;
80 }
81
82 bool
83 GetCurrentDir(char *str, size_t size)
84 {
85     char *ret;
86     ret = getcwd(str, size);
87     str[size - 1] = 0;
88     return ret ? true : false;
89 }
90
91 void
92 DebugMessage(const char *format, ...)
93 {
94     va_list ap;
95     va_start(ap, format);
96     fflush(stdout);
97     vfprintf(stderr, format, ap);
98     va_end(ap);
99 }
100
101 long long GetTime(void)
102 {
103     struct timeval tv;
104     gettimeofday(&tv, NULL);
105     return tv.tv_usec + tv.tv_sec*1000000LL;
106 }
107
108 void
109 Abort(void)
110 {
111     exit(0);
112 }
113
114
115 } /* namespace OS */
116