Share file offset across processes

This change ensures that if a file descriptor for an open disk file gets
shared by multiple processes within a process tree, then lseek() changes
will be visible across processes, and read() / write() are synchronized.
Note this only applies to Windows, because UNIX kernels already do this.
This commit is contained in:
Justine Tunney 2024-08-03 01:24:46 -07:00
parent a80ab3f8fe
commit 761c6ad615
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
15 changed files with 256 additions and 63 deletions

View file

@ -17,6 +17,8 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/state.internal.h"
#include "libc/intrin/fds.h"
#include "libc/runtime/runtime.h"
#include "libc/thread/thread.h"
void __fds_lock(void) {
@ -26,3 +28,23 @@ void __fds_lock(void) {
void __fds_unlock(void) {
pthread_mutex_unlock(&__fds_lock_obj);
}
void __fd_lock(struct Fd *f) {
pthread_mutex_lock(&f->shared->lock);
}
void __fd_unlock(struct Fd *f) {
pthread_mutex_unlock(&f->shared->lock);
}
struct Cursor *__cursor_new(void) {
struct Cursor *c;
if ((c = _mapshared(sizeof(struct Cursor)))) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&c->lock, &attr);
pthread_mutexattr_destroy(&attr);
}
return c;
}