Commit Graph

38 Commits

Author SHA1 Message Date
bjorn cdecc1bae7 Add defer system; Rework exceptions; util cleanup;
Defer can be used to clean up resources when an error occurs.
2024-03-29 18:10:22 -07:00
bjorn d097d9819d Add wrappers for malloc functions; 2024-03-11 14:38:00 -07:00
bjorn 2d7b636a90 Change more asserts to checks; 2024-03-11 00:58:21 -07:00
bjorn b32ac1c5a5 Details; 2024-02-28 12:31:34 -08:00
bjorn a4c1d9fce6 Thread module initializes job system;
Worker count can be set from conf.lua.

A negative worker count is relative to the number of cores.

-1 is the default.
2024-02-28 12:31:34 -08:00
bjorn 3a2d453ce8 Fix thread module destruction; 2023-12-03 06:34:22 -08:00
bjorn 08d6b2ad28 Refcounted modules;
This allows them to be initialized/destroyed from multiple threads in
any order.  Previously, the first thread to require a module had to be
the last thread to use the module, otherwise it would be destroyed too
early.

There are still a few issues.  If the main thread doesn't require a
module, it won't pick up the conf.lua settings.  Also graphics isn't
handling the shader cache writing properly.  And I think this breaks the
headset-graphics refcounting.  But these will be fixed in future
commits.
2023-11-23 17:07:44 -08:00
bjorn 1d91ea1fde Replace tinycthread with C11 threads.h polyfill; 2023-09-21 23:26:16 -07:00
bjorn 14cb2ed368 Add os_thread_attach/os_thread_detach;
These are called when creating/destroying Thread objects.  It's
currently only implemented on Android, where it attaches/detaches the
Java VM to the thread.  This allows JNI calls to be used on threads.

However I don't think e.g. `lovr.system.requestPermission` will work on
a thread yet, because it uses the global JNI env from the main thread
instead of the thread's JNI env.  Still, attaching/detaching the VM is
an improvement and will allow well-behaved JNI methods to work on
threads now.

I don't know how expensive this is, yolo.
2023-09-05 16:20:32 -07:00
bjorn ad4978f692 Fix prototypes; 2023-04-25 21:37:14 -07:00
bjorn ffa4beb21c Fix thread refcounting;
Minor race condition between thread starting and thread getting released.
2023-01-19 15:05:04 -08:00
bjorn f013831b73 Reorganize thread code;
- Put channel into thread module file.
- Make thread internals private.
- Handle more thread bookkeeping in thread module instead of Lua API.
- Fix a few race conditions/leaks nobody was probably ever going to hit.
2022-12-10 20:13:39 -08:00
Ilya 10675761e2
Check for thread handle 2022-12-11 00:47:20 +03:00
bjorn 1c9adea2e2 Simplify channel hash table; 2022-09-10 23:59:14 -07:00
bjorn 5d271de91a mv map util; 2022-03-30 12:34:38 -07:00
bjorn 2da18d419b mv util src; 2022-03-22 00:13:38 -07:00
bjorn c9fe026a66 Fix undefined behavior in arr_free;
Currently there is a single allocator function used in arr_t.  Its
behavior depends on the values for the pointer and size arguments:

- If pointer is NULL, it should allocate new memory.
- If pointer is non-NULL and size is positive, it should resize memory.
- If size is zero, it should free memory.

All instances of arr_t use realloc for this right now.  The problem
is that realloc's behavior is undefined when the size argument is zero.
On Windows and Linux, realloc will free the pointer, but on macOS this
isn't the case.  This means that arr_t leaks memory on macOS.

It's best to not rely on undefined behavior like this, so let's instead
use a helper function that behaves the way we want.
2022-03-14 12:27:58 -07:00
bjorn 023067ec27 util.h no longer uses atomics; 2021-02-11 16:37:55 -07:00
bjorn 6d92d54079 Try having arr_t in util; 2021-02-08 18:25:05 -07:00
bjorn bc4cde1653 Adjust lovrRelease signature; 2021-02-08 17:52:26 -07:00
bjorn 3ded60948f rm core/ref; rm lovrAlloc; util does refcounting; 2021-02-08 17:26:44 -07:00
bjorn ed09bc3cfa rm unnecessary newlines in assert messages; 2021-02-07 16:58:50 -07:00
bjorn 1e7749a58a Fix MSVC warnings;
It's that time of year.
2020-11-12 18:08:05 -07:00
bjorn 75e26f0051 Move core/hash to core/util; 2020-05-19 13:49:40 -06:00
bjorn 2a52796c6f Commenting; 2020-01-15 17:56:09 -08:00
bjorn 056c1e7fe4 Simplify Channel cleanup; 2020-01-13 23:49:52 -08:00
bjorn 46c685bd27 Make channel creation threadsafe; 2020-01-13 23:49:52 -08:00
bjorn e2f67c106d Thread:start supports up to 4 arguments;
They are passed to the chunk.
2019-12-13 15:50:10 -08:00
bjorn 2f9400a5f7 Adjust includes; 2019-12-10 13:01:28 -08:00
bjorn 13136482b9 Fix Channel memory leak;
Channels need to be removed from the global array/map when destroyed.

Note that this exposes an infinite loop in map_remove, which will
be fixed later.

Note also that Channel's are retained if they have any messages in
them, to prevent releasing a channel while it has pending messages.
2019-11-11 18:33:33 -08:00
bjorn d21911d010 Add core/map hash table; rm lib/map; 2019-10-30 00:07:05 -07:00
bjorn 28344928c9 Simplify arr; 2019-08-21 23:16:58 -07:00
bjorn 6b87576e6d Channel: cleanup; 2019-07-11 11:29:18 -07:00
bjorn 7ebee200ea rm strdup;
It isn't part of C, causing compiler errors on some platforms.
2019-06-20 22:21:31 -07:00
bjorn eb1e257209 Add new array implementation; Upgrade filesystem;
Filesystem:

- Uses streaming file IO.
- Uses less memory when requiring files.
- Simplifies its require path implementation.
2019-06-16 13:30:30 -07:00
bjorn 22fe333150 Update refcounting (again);
- Ref struct only stores refcount now and is more general.
- Proxy stores a hash of its type name instead of an enum.
- Variants store additional information instead of using a vtable.
- Remove the concept of superclasses from the API.
- Clean up some miscellaneous includes.
2019-06-02 01:02:26 -07:00
bjorn df6b7fc96a Ref is mostly private now;
It's really nice how objects don't need to care about how they're
allocated/managed now.
2019-05-20 15:09:06 -07:00
bjorn a17f10e273 Add modules folder; 2019-05-19 00:38:35 -07:00