---
# System prepended metadata

title: Untitled

---

### Threads impl details
Current idea: Start with option 1 then transition to option 2 once new capabilities are added to the Wasm instructions set.

Depending on Wasm Standards evolution, this transition could be seamless (see Future Directions section).

### Option 1: A single thread per Wasm instance.
* Easier to implement (some VMs have this approach)
* Behavior that may need to access global data and modify it. Are there any proposed WASI APIs that would break if we implement it this way?
* Consumes extra memory (duplicated the Wasm instance for each thread).
* 

### Option 2 (The Right Way): Multiple threads per Wasm instance.
This seems to not be possible today due to browser restrictions.
To satisfy those, we need the extensions presented in the weakening wasm paper.
(pretend it's `shared`)

### Possible future directions of Wasm spec

* `shared/private` (`local` in the paper) attrs get added to the spec.
* Only Shared gets added, no attr means `private` to the instance - best case
* Only Private gets added (this seems highly unlikely)
* Nothing gets added
* Both get added



### API
*Keep only create, rename it to fork, 2 params: func, arg, flag 0 for now*
* call into some named export in the instance, that then redirects to the function and fwds the param.* (add a new func _thread_start(arg(contains table idx)))
* or have the table exported and call into it from there* 


```cpp
// Thread creation/join/destruction APIs:
int pthread_create(pthread_t *thread, const void *attr,
                   void *(*start_routine) (void *), void *arg);

int pthread_join(pthread_t thread, void **retval);

int pthread_detach(pthread_t thread);

int pthread_cancel(pthread_t thread);

pthread_t pthread_self(void);

void pthread_exit(void *retval);

int pthread_yield(void);

// Mutexes:

int pthread_mutex_init(pthread_mutex_t *mutex, const void *attr);

int pthread_mutex_lock(pthread_mutex_t *mutex);

int pthread_mutex_unlock(pthread_mutex_t *mutex);

int pthread_mutex_destroy(pthread_mutex_t *mutex);

// RW Locks

int pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr);

int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);

int pthread_rwlock_unlock();

int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);


// Conditionals:
int pthread_cond_init(pthread_cond_t *cond, const void *attr);

int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex);

int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, unsigned int useconds);

int pthread_cond_signal(pthread_cond_t *cond);

int pthread_cond_broadcast(pthread_cond_t *cond);

int pthread_cond_destroy(pthread_cond_t *cond);

// Thread-specific data:

int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));

int pthread_setspecific(pthread_key_t key, const void *value);

void *pthread_getspecific(pthread_key_t key);

int pthread_key_delete(pthread_key_t key);
```