Skip to content

Commit a8011e9

Browse files
Adding new files
1 parent 02faca1 commit a8011e9

23 files changed

+4598
-0
lines changed

crn_linux.workspace

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
2+
<CodeBlocks_workspace_file>
3+
<Workspace title="Workspace">
4+
<Project filename="crunch/crunch_linux.cbp" active="1">
5+
<Depends filename="crnlib/crnlib_linux.cbp" />
6+
</Project>
7+
<Project filename="crnlib/crnlib_linux.cbp" />
8+
</Workspace>
9+
</CodeBlocks_workspace_file>

crnlib/crn_atomics.h

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
// File: crn_atomics.h
2+
#ifndef CRN_ATOMICS_H
3+
#define CRN_ATOMICS_H
4+
5+
#ifdef WIN32
6+
#pragma once
7+
#endif
8+
9+
#ifdef WIN32
10+
#include "crn_winhdr.h"
11+
#endif
12+
13+
#if defined(__GNUC__) && CRNLIB_PLATFORM_PC
14+
extern __inline__ __attribute__((__always_inline__,__gnu_inline__)) void crnlib_yield_processor()
15+
{
16+
__asm__ __volatile__("pause");
17+
}
18+
#else
19+
CRNLIB_FORCE_INLINE void crnlib_yield_processor()
20+
{
21+
#if CRNLIB_USE_MSVC_INTRINSICS
22+
#if CRNLIB_PLATFORM_PC_X64
23+
_mm_pause();
24+
#else
25+
YieldProcessor();
26+
#endif
27+
#else
28+
// No implementation
29+
#endif
30+
}
31+
#endif
32+
33+
#if CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS
34+
extern "C" __int64 _InterlockedCompareExchange64(__int64 volatile * Destination, __int64 Exchange, __int64 Comperand);
35+
#if defined(_MSC_VER)
36+
#pragma intrinsic(_InterlockedCompareExchange64)
37+
#endif
38+
#endif // CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS
39+
40+
namespace crnlib
41+
{
42+
#if CRNLIB_USE_WIN32_ATOMIC_FUNCTIONS
43+
typedef LONG atomic32_t;
44+
typedef LONGLONG atomic64_t;
45+
46+
// Returns the original value.
47+
inline atomic32_t atomic_compare_exchange32(atomic32_t volatile *pDest, atomic32_t exchange, atomic32_t comparand)
48+
{
49+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
50+
return InterlockedCompareExchange(pDest, exchange, comparand);
51+
}
52+
53+
// Returns the original value.
54+
inline atomic64_t atomic_compare_exchange64(atomic64_t volatile *pDest, atomic64_t exchange, atomic64_t comparand)
55+
{
56+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 7) == 0);
57+
return _InterlockedCompareExchange64(pDest, exchange, comparand);
58+
}
59+
60+
// Returns the resulting incremented value.
61+
inline atomic32_t atomic_increment32(atomic32_t volatile *pDest)
62+
{
63+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
64+
return InterlockedIncrement(pDest);
65+
}
66+
67+
// Returns the resulting decremented value.
68+
inline atomic32_t atomic_decrement32(atomic32_t volatile *pDest)
69+
{
70+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
71+
return InterlockedDecrement(pDest);
72+
}
73+
74+
// Returns the original value.
75+
inline atomic32_t atomic_exchange32(atomic32_t volatile *pDest, atomic32_t val)
76+
{
77+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
78+
return InterlockedExchange(pDest, val);
79+
}
80+
81+
// Returns the resulting value.
82+
inline atomic32_t atomic_add32(atomic32_t volatile *pDest, atomic32_t val)
83+
{
84+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
85+
return InterlockedExchangeAdd(pDest, val) + val;
86+
}
87+
88+
// Returns the original value.
89+
inline atomic32_t atomic_exchange_add32(atomic32_t volatile *pDest, atomic32_t val)
90+
{
91+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
92+
return InterlockedExchangeAdd(pDest, val);
93+
}
94+
#elif CRNLIB_USE_GCC_ATOMIC_BUILTINS
95+
typedef long atomic32_t;
96+
typedef long long atomic64_t;
97+
98+
// Returns the original value.
99+
inline atomic32_t atomic_compare_exchange32(atomic32_t volatile *pDest, atomic32_t exchange, atomic32_t comparand)
100+
{
101+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
102+
return __sync_val_compare_and_swap(pDest, comparand, exchange);
103+
}
104+
105+
// Returns the original value.
106+
inline atomic64_t atomic_compare_exchange64(atomic64_t volatile *pDest, atomic64_t exchange, atomic64_t comparand)
107+
{
108+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 7) == 0);
109+
return __sync_val_compare_and_swap(pDest, comparand, exchange);
110+
}
111+
112+
// Returns the resulting incremented value.
113+
inline atomic32_t atomic_increment32(atomic32_t volatile *pDest)
114+
{
115+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
116+
return __sync_add_and_fetch(pDest, 1);
117+
}
118+
119+
// Returns the resulting decremented value.
120+
inline atomic32_t atomic_decrement32(atomic32_t volatile *pDest)
121+
{
122+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
123+
return __sync_sub_and_fetch(pDest, 1);
124+
}
125+
126+
// Returns the original value.
127+
inline atomic32_t atomic_exchange32(atomic32_t volatile *pDest, atomic32_t val)
128+
{
129+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
130+
return __sync_lock_test_and_set(pDest, val);
131+
}
132+
133+
// Returns the resulting value.
134+
inline atomic32_t atomic_add32(atomic32_t volatile *pDest, atomic32_t val)
135+
{
136+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
137+
return __sync_add_and_fetch(pDest, val);
138+
}
139+
140+
// Returns the original value.
141+
inline atomic32_t atomic_exchange_add32(atomic32_t volatile *pDest, atomic32_t val)
142+
{
143+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
144+
return __sync_fetch_and_add(pDest, val);
145+
}
146+
#else
147+
#define CRNLIB_NO_ATOMICS 1
148+
149+
// Atomic ops not supported - but try to do something reasonable. Assumes no threading at all.
150+
typedef long atomic32_t;
151+
typedef long long atomic64_t;
152+
153+
inline atomic32_t atomic_compare_exchange32(atomic32_t volatile *pDest, atomic32_t exchange, atomic32_t comparand)
154+
{
155+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
156+
atomic32_t cur = *pDest;
157+
if (cur == comparand)
158+
*pDest = exchange;
159+
return cur;
160+
}
161+
162+
inline atomic64_t atomic_compare_exchange64(atomic64_t volatile *pDest, atomic64_t exchange, atomic64_t comparand)
163+
{
164+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 7) == 0);
165+
atomic64_t cur = *pDest;
166+
if (cur == comparand)
167+
*pDest = exchange;
168+
return cur;
169+
}
170+
171+
inline atomic32_t atomic_increment32(atomic32_t volatile *pDest)
172+
{
173+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
174+
return (*pDest += 1);
175+
}
176+
177+
inline atomic32_t atomic_decrement32(atomic32_t volatile *pDest)
178+
{
179+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
180+
return (*pDest -= 1);
181+
}
182+
183+
inline atomic32_t atomic_exchange32(atomic32_t volatile *pDest, atomic32_t val)
184+
{
185+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
186+
atomic32_t cur = *pDest;
187+
*pDest = val;
188+
return cur;
189+
}
190+
191+
inline atomic32_t atomic_add32(atomic32_t volatile *pDest, atomic32_t val)
192+
{
193+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
194+
return (*pDest += val);
195+
}
196+
197+
inline atomic32_t atomic_exchange_add32(atomic32_t volatile *pDest, atomic32_t val)
198+
{
199+
CRNLIB_ASSERT((reinterpret_cast<ptr_bits_t>(pDest) & 3) == 0);
200+
atomic32_t cur = *pDest;
201+
*pDest += val;
202+
return cur;
203+
}
204+
#endif
205+
206+
} // namespace crnlib
207+
208+
#endif // CRN_ATOMICS_H

crnlib/crn_colorized_console.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// File: crn_colorized_console.cpp
2+
// See Copyright Notice and license at the end of inc/crnlib.h
3+
#include "crn_core.h"
4+
#include "crn_colorized_console.h"
5+
#ifdef CRNLIB_USE_WIN32_API
6+
#include "crn_winhdr.h"
7+
#endif
8+
9+
namespace crnlib
10+
{
11+
void colorized_console::init()
12+
{
13+
console::init();
14+
console::add_console_output_func(console_output_func, NULL);
15+
}
16+
17+
void colorized_console::deinit()
18+
{
19+
console::remove_console_output_func(console_output_func);
20+
console::deinit();
21+
}
22+
23+
void colorized_console::tick()
24+
{
25+
}
26+
27+
#ifdef CRNLIB_USE_WIN32_API
28+
bool colorized_console::console_output_func(eConsoleMessageType type, const char* pMsg, void* pData)
29+
{
30+
pData;
31+
32+
if (console::get_output_disabled())
33+
return true;
34+
35+
HANDLE cons = GetStdHandle(STD_OUTPUT_HANDLE);
36+
37+
DWORD attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
38+
switch (type)
39+
{
40+
case cDebugConsoleMessage: attr = FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
41+
case cMessageConsoleMessage: attr = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break;
42+
case cWarningConsoleMessage: attr = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; break;
43+
case cErrorConsoleMessage: attr = FOREGROUND_RED | FOREGROUND_INTENSITY; break;
44+
default: break;
45+
}
46+
47+
if (INVALID_HANDLE_VALUE != cons)
48+
SetConsoleTextAttribute(cons, (WORD)attr);
49+
50+
if (console::get_prefixes())
51+
{
52+
switch (type)
53+
{
54+
case cDebugConsoleMessage:
55+
printf("Debug: %s", pMsg);
56+
break;
57+
case cWarningConsoleMessage:
58+
printf("Warning: %s", pMsg);
59+
break;
60+
case cErrorConsoleMessage:
61+
printf("Error: %s", pMsg);
62+
break;
63+
default:
64+
printf("%s", pMsg);
65+
break;
66+
}
67+
}
68+
else
69+
{
70+
printf("%s", pMsg);
71+
}
72+
73+
if (console::get_crlf())
74+
printf("\n");
75+
76+
if (INVALID_HANDLE_VALUE != cons)
77+
SetConsoleTextAttribute(cons, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
78+
79+
return true;
80+
}
81+
#else
82+
bool colorized_console::console_output_func(eConsoleMessageType type, const char* pMsg, void* pData)
83+
{
84+
pData;
85+
if (console::get_output_disabled())
86+
return true;
87+
88+
if (console::get_prefixes())
89+
{
90+
switch (type)
91+
{
92+
case cDebugConsoleMessage:
93+
printf("Debug: %s", pMsg);
94+
break;
95+
case cWarningConsoleMessage:
96+
printf("Warning: %s", pMsg);
97+
break;
98+
case cErrorConsoleMessage:
99+
printf("Error: %s", pMsg);
100+
break;
101+
default:
102+
printf("%s", pMsg);
103+
break;
104+
}
105+
}
106+
else
107+
{
108+
printf("%s", pMsg);
109+
}
110+
111+
if (console::get_crlf())
112+
printf("\n");
113+
114+
return true;
115+
}
116+
#endif
117+
118+
} // namespace crnlib
119+

crnlib/crn_colorized_console.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// File: crn_colorized_console.h
2+
// See Copyright Notice and license at the end of inc/crnlib.h
3+
#pragma once
4+
#include "crn_console.h"
5+
6+
namespace crnlib
7+
{
8+
class colorized_console
9+
{
10+
public:
11+
static void init();
12+
static void deinit();
13+
static void tick();
14+
15+
private:
16+
static bool console_output_func(eConsoleMessageType type, const char* pMsg, void* pData);
17+
};
18+
19+
} // namespace crnlib

0 commit comments

Comments
 (0)