aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorcomex2015-01-29 01:48:09 -0500
committercomex2015-01-29 01:48:17 -0500
commit5d7d4cdf531234390055540dc4ccc478268b0588 (patch)
tree4bda0d50e2284986d730efe7ce9db77a28f51b3f /lib
parentport some old code (diff)
downloadsubstitute-5d7d4cdf531234390055540dc4ccc478268b0588.tar.gz
get rid of the unnecessary CoreFoundation dependency
Diffstat (limited to 'lib')
-rw-r--r--lib/cbit/htab.h19
-rw-r--r--lib/darwin/stop-other-threads.c82
2 files changed, 56 insertions, 45 deletions
diff --git a/lib/cbit/htab.h b/lib/cbit/htab.h
index 861a9be..5c9b993 100644
--- a/lib/cbit/htab.h
+++ b/lib/cbit/htab.h
@@ -95,7 +95,6 @@ struct htab_internal {
key_ty *bucket = (void *) (buckets + i * entry_size); \
if (null_func(bucket)) { \
if (add) { \
- *bucket = *key; \
hi->length++; \
return bucket; \
} else { \
@@ -213,15 +212,25 @@ struct htab_internal {
} \
UNUSED_STATIC_INLINE \
bucket_ty *htab_setbucket_##name(htab_ty *restrict ht, \
- const key_ty *restrict key) { \
+ const key_ty *restrict key) { \
return __htab_key_lookup_##key_name(&ht->hi, key, sizeof(bucket_ty), \
true, true); \
} \
UNUSED_STATIC_INLINE \
value_ty *htab_setp_##name(const htab_ty *restrict ht, \
- const key_ty *restrict key) { \
+ const key_ty *restrict key, \
+ bool *new_p) { \
bucket_ty *bucket = htab_setbucket_##name((void *) ht, key); \
- return bucket ? &bucket->value : NULL; \
+ bool new = false; \
+ if (__htab_key_is_null_##key_name(&bucket->key)) { \
+ bucket->key = *key; \
+ new = true; \
+ } else { \
+ new = false; \
+ } \
+ if (new_p) \
+ *new_p = new; \
+ return &bucket->value; \
} \
UNUSED_STATIC_INLINE \
bool htab_remove_##name(htab_ty *restrict ht, const key_ty *restrict key) { \
@@ -240,7 +249,7 @@ struct htab_internal {
return __htab_key_resize_##key_name(&ht->hi, size, sizeof(bucket_ty)); \
} \
UNUSED_STATIC_INLINE \
- void htab_free_##name(htab_ty *ht) { \
+ void htab_free_storage_##name(htab_ty *ht) { \
if (ht->base != ht->storage) \
free(ht->base); \
}
diff --git a/lib/darwin/stop-other-threads.c b/lib/darwin/stop-other-threads.c
index ee9e803..1a4f43f 100644
--- a/lib/darwin/stop-other-threads.c
+++ b/lib/darwin/stop-other-threads.c
@@ -1,19 +1,17 @@
-#if 0
#include "substitute.h"
#include "substitute-internal.h"
#include "darwin/mach-decls.h"
+#include "stop-other-threads.h"
+#include "cbit/htab.h"
#include <pthread.h>
#include <mach/mach.h>
-static void release_port(UNUSED CFAllocatorRef allocator, const void *value) {
- mach_port_t thread = (mach_port_t) value;
- thread_resume(thread);
- mach_port_deallocate(mach_task_self(), thread);
-}
-static CFSetCallBacks suspend_port_callbacks = {
- .version = 0,
- .release = release_port,
-};
+#define port_hash(portp) (*(portp))
+#define port_eq(port1p, port2p) (*(port1p) == *(port2p))
+#define port_null(portp) (*(portp) == MACH_PORT_NULL)
+DECL_STATIC_HTAB_KEY(mach_port_t, mach_port_t, port_hash, port_eq, port_null, 0);
+struct empty {};
+DECL_HTAB(mach_port_set, mach_port_t, struct empty);
static bool apply_one_pcp(mach_port_t thread,
uintptr_t (*callback)(void *ctx, uintptr_t pc),
@@ -69,27 +67,6 @@ static bool apply_one_pcp(mach_port_t thread,
return true;
}
-int apply_pc_patch_callback(void *token,
- uintptr_t (*pc_patch_callback)(void *ctx, uintptr_t pc),
- void *ctx) {
- CFMutableSetRef suspended_set = token;
- CFIndex count = CFSetGetCount(suspended_set);
- if (!count)
- return SUBSTITUTE_OK;
- /* great API there CF */
- const void **ports = malloc(sizeof(*ports) * count);
- CFSetGetValues(suspended_set, ports);
- int ret = SUBSTITUTE_OK;
- for (CFIndex i = 0; i < count; i++) {
- if (!apply_one_pcp((mach_port_t) ports[i], pc_patch_callback, ctx)) {
- ret = SUBSTITUTE_ERR_ADJUSTING_THREADS;
- break;
- }
- }
- free(ports);
- return ret;
-}
-
int stop_other_threads(void **token_ptr) {
if (!pthread_main_np())
return SUBSTITUTE_ERR_NOT_ON_MAIN_THREAD;
@@ -101,7 +78,9 @@ int stop_other_threads(void **token_ptr) {
* created while we're looping, without suspending anything twice. Keep
* looping until only threads we already suspended before this loop are
* there. */
- CFMutableSetRef suspended_set = CFSetCreateMutable(NULL, 0, &suspend_port_callbacks);
+ HTAB_STORAGE(mach_port_set) *hs = malloc(sizeof(*hs));
+ HTAB_STORAGE_INIT(hs, mach_port_set);
+ struct htab_mach_port_set *suspended_set = &hs->h;
thread_act_array_t ports = 0;
mach_msg_type_number_t nports = 0;
@@ -118,14 +97,14 @@ int stop_other_threads(void **token_ptr) {
for (mach_msg_type_number_t i = 0; i < nports; i++) {
mach_port_t port = ports[i];
- void *casted_port = (void *) (uintptr_t) port;
+ struct htab_bucket_mach_port_set *bucket;
if (port == self ||
- CFSetContainsValue(suspended_set, casted_port)) {
+ (bucket = htab_setbucket_mach_port_set(suspended_set, &port),
+ bucket->key)) {
/* already suspended, ignore */
mach_port_deallocate(mach_task_self(), port);
} else {
got_new = true;
- printf("suspending %d (self=%d)\n", port, self);
kr = thread_suspend(port);
if (kr == KERN_TERMINATED) {
/* too late */
@@ -138,7 +117,7 @@ int stop_other_threads(void **token_ptr) {
nports * sizeof(*ports));
goto fail;
}
- CFSetAddValue(suspended_set, casted_port);
+ bucket->key = port;
}
}
vm_deallocate(mach_task_self(), (vm_address_t) ports,
@@ -150,13 +129,36 @@ int stop_other_threads(void **token_ptr) {
return SUBSTITUTE_OK;
fail:
- CFRelease(suspended_set);
+ resume_other_threads(suspended_set);
+ return ret;
+}
+
+int apply_pc_patch_callback(void *token,
+ uintptr_t (*pc_patch_callback)(void *ctx, uintptr_t pc),
+ void *ctx) {
+ struct htab_mach_port_set *suspended_set = token;
+ int ret = SUBSTITUTE_OK;
+ HTAB_FOREACH(suspended_set, mach_port_t *threadp,
+ UNUSED struct empty *_,
+ mach_port_set) {
+ if (!apply_one_pcp(*threadp, pc_patch_callback, ctx)) {
+ ret = SUBSTITUTE_ERR_ADJUSTING_THREADS;
+ break;
+ }
+ }
return ret;
}
+
int resume_other_threads(void *token) {
- CFMutableSetRef suspended_set = token;
- CFRelease(suspended_set);
+ struct htab_mach_port_set *suspended_set = token;
+ HTAB_FOREACH(suspended_set, mach_port_t *threadp,
+ UNUSED struct empty *_,
+ mach_port_set) {
+ thread_resume(*threadp);
+ mach_port_deallocate(mach_task_self(), *threadp);
+ }
+ htab_free_storage_mach_port_set(suspended_set);
+ free(suspended_set);
return SUBSTITUTE_OK; /* eh */
}
-#endif