Skip to content

Commit c57925d

Browse files
committed
---
yaml --- r: 11883 b: refs/heads/master c: b278d67 h: refs/heads/master i: 11881: f7266ba 11879: 4918e0e v: v3
1 parent e2ab205 commit c57925d

File tree

7 files changed

+69
-71
lines changed

7 files changed

+69
-71
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 337d860a8777a761267daaad9b561787b10e7c87
2+
refs/heads/master: b278d675a231fdfe825c72e499d59e8a3d07ffaa
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
55
refs/heads/try: 2898dcc5d97da9427ac367542382b6239d9c0bbf

trunk/src/rt/rust_builtin.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -513,27 +513,20 @@ get_port_id(rust_port *port) {
513513
extern "C" CDECL uintptr_t
514514
chan_id_send(type_desc *t, rust_task_id target_task_id,
515515
rust_port_id target_port_id, void *sptr) {
516-
// FIXME: make sure this is thread-safe
517516
bool sent = false;
518517
rust_task *task = rust_task_thread::get_task();
519518

520519
LOG(task, comm, "chan_id_send task: 0x%" PRIxPTR
521520
" port: 0x%" PRIxPTR, (uintptr_t) target_task_id,
522521
(uintptr_t) target_port_id);
523522

524-
rust_task *target_task = task->kernel->get_task_by_id(target_task_id);
525-
if(target_task) {
526-
rust_port *port = target_task->get_port_by_id(target_port_id);
527-
if(port) {
528-
port->send(sptr);
529-
port->deref();
530-
sent = true;
531-
} else {
532-
LOG(task, comm, "didn't get the port");
533-
}
534-
target_task->deref();
523+
rust_port *port = task->kernel->get_port_by_id(target_port_id);
524+
if(port) {
525+
port->send(sptr);
526+
port->deref();
527+
sent = true;
535528
} else {
536-
LOG(task, comm, "didn't get the task");
529+
LOG(task, comm, "didn't get the port");
537530
}
538531
return (uintptr_t)sent;
539532
}

trunk/src/rt/rust_kernel.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rust_kernel::rust_kernel(rust_srv *srv) :
1818
_log(srv, NULL),
1919
srv(srv),
2020
max_task_id(0),
21+
max_port_id(0),
2122
rval(0),
2223
max_sched_id(0),
2324
env(srv->env)
@@ -212,6 +213,46 @@ rust_kernel::get_task_by_id(rust_task_id id) {
212213
return task;
213214
}
214215

216+
rust_port_id
217+
rust_kernel::register_port(rust_port *port) {
218+
uintptr_t new_live_ports;
219+
rust_port_id new_port_id;
220+
{
221+
scoped_lock with(port_lock);
222+
new_port_id = max_port_id++;
223+
port_table.put(new_port_id, port);
224+
new_live_ports = port_table.count();
225+
}
226+
K(srv, new_port_id != INTPTR_MAX, "Hit the maximum port id");
227+
KLOG_("Registered port %" PRIdPTR, new_port_id);
228+
KLOG_("Total outstanding ports: %d", new_live_ports);
229+
return new_port_id;
230+
}
231+
232+
void
233+
rust_kernel::release_port_id(rust_port_id id) {
234+
KLOG_("Releasing port %" PRIdPTR, id);
235+
uintptr_t new_live_ports;
236+
{
237+
scoped_lock with(port_lock);
238+
port_table.remove(id);
239+
new_live_ports = port_table.count();
240+
}
241+
KLOG_("Total outstanding ports: %d", new_live_ports);
242+
}
243+
244+
rust_port *
245+
rust_kernel::get_port_by_id(rust_port_id id) {
246+
scoped_lock with(port_lock);
247+
rust_port *port = NULL;
248+
// get leaves port unchanged if not found.
249+
port_table.get(id, &port);
250+
if(port) {
251+
port->ref();
252+
}
253+
return port;
254+
}
255+
215256
#ifdef __WIN32__
216257
void
217258
rust_kernel::win32_require(LPCTSTR fn, BOOL ok) {

trunk/src/rt/rust_kernel.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class rust_kernel {
3030
rust_task_id max_task_id;
3131
hash_map<rust_task_id, rust_task *> task_table;
3232

33+
// Protects max_port_id and port_table
34+
lock_and_signal port_lock;
35+
// The next port id
36+
rust_task_id max_port_id;
37+
hash_map<rust_port_id, rust_port *> port_table;
38+
3339
lock_and_signal rval_lock;
3440
int rval;
3541

@@ -73,6 +79,10 @@ class rust_kernel {
7379
rust_task *get_task_by_id(rust_task_id id);
7480
void release_task_id(rust_task_id tid);
7581

82+
rust_port_id register_port(rust_port *port);
83+
rust_port *get_port_by_id(rust_port_id id);
84+
void release_port_id(rust_port_id tid);
85+
7686
void set_exit_status(int code);
7787
};
7888

trunk/src/rt/rust_port.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ rust_port::rust_port(rust_task *task, size_t unit_sz)
1111
PRIxPTR, (uintptr_t)task, unit_sz, (uintptr_t)this);
1212

1313
task->ref();
14-
id = task->register_port(this);
14+
id = kernel->register_port(this);
1515
}
1616

1717
rust_port::~rust_port() {
@@ -39,7 +39,7 @@ void rust_port::deref() {
3939
void rust_port::begin_detach(uintptr_t *yield) {
4040
*yield = false;
4141

42-
task->release_port(id);
42+
kernel->release_port_id(id);
4343

4444
scoped_lock with(ref_lock);
4545
ref_count--;

trunk/src/rt/rust_task.cpp

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ rust_task::rust_task(rust_task_thread *thread, rust_task_list *state,
7575
kernel(thread->kernel),
7676
name(name),
7777
list_index(-1),
78-
next_port_id(0),
7978
rendezvous_ptr(0),
8079
local_region(&thread->srv->local_region),
8180
boxed(&local_region),
@@ -107,11 +106,6 @@ rust_task::rust_task(rust_task_thread *thread, rust_task_list *state,
107106
void
108107
rust_task::delete_this()
109108
{
110-
{
111-
scoped_lock with (port_lock);
112-
I(thread, port_table.is_empty());
113-
}
114-
115109
DLOG(thread, task, "~rust_task %s @0x%" PRIxPTR ", refcnt=%d",
116110
name, (uintptr_t)this, ref_count);
117111

@@ -476,49 +470,19 @@ rust_task::calloc(size_t size, const char *tag) {
476470
return local_region.calloc(size, tag);
477471
}
478472

479-
rust_port_id rust_task::register_port(rust_port *port) {
480-
I(thread, !port_lock.lock_held_by_current_thread());
481-
scoped_lock with(port_lock);
482-
483-
rust_port_id id = next_port_id++;
484-
A(thread, id != INTPTR_MAX, "Hit the maximum port id");
485-
port_table.put(id, port);
486-
return id;
487-
}
488-
489-
void rust_task::release_port(rust_port_id id) {
490-
scoped_lock with(port_lock);
491-
port_table.remove(id);
492-
}
493-
494-
rust_port *rust_task::get_port_by_id(rust_port_id id) {
495-
I(thread, !port_lock.lock_held_by_current_thread());
496-
scoped_lock with(port_lock);
497-
rust_port *port = NULL;
498-
port_table.get(id, &port);
499-
if (port) {
500-
port->ref();
501-
}
502-
return port;
503-
}
504-
505473
void
506474
rust_task::notify(bool success) {
507475
// FIXME (1078) Do this in rust code
508476
if(notify_enabled) {
509-
rust_task *target_task = kernel->get_task_by_id(notify_chan.task);
510-
if (target_task) {
511-
rust_port *target_port =
512-
target_task->get_port_by_id(notify_chan.port);
513-
if(target_port) {
514-
task_notification msg;
515-
msg.id = id;
516-
msg.result = !success ? tr_failure : tr_success;
517-
518-
target_port->send(&msg);
519-
target_port->deref();
520-
}
521-
target_task->deref();
477+
rust_port *target_port =
478+
kernel->get_port_by_id(notify_chan.port);
479+
if(target_port) {
480+
task_notification msg;
481+
msg.id = id;
482+
msg.result = !success ? tr_failure : tr_success;
483+
484+
target_port->send(&msg);
485+
target_port->deref();
522486
}
523487
}
524488
}

trunk/src/rt/rust_task.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ rust_task : public kernel_owned<rust_task>, rust_cond
7070
const char *const name;
7171
int32_t list_index;
7272

73-
rust_port_id next_port_id;
74-
7573
// Rendezvous pointer for receiving data when blocked on a port. If we're
7674
// trying to read data and no data is available on any incoming channel,
7775
// we block on the port, and yield control to the scheduler. Since, we
@@ -100,10 +98,6 @@ rust_task : public kernel_owned<rust_task>, rust_cond
10098

10199
private:
102100

103-
// Protects port_table
104-
lock_and_signal port_lock;
105-
hash_map<rust_port_id, rust_port *> port_table;
106-
107101
// Protects state, cond, cond_name
108102
lock_and_signal state_lock;
109103
rust_task_list *state;
@@ -201,10 +195,6 @@ rust_task : public kernel_owned<rust_task>, rust_cond
201195

202196
void *calloc(size_t size, const char *tag);
203197

204-
rust_port_id register_port(rust_port *port);
205-
void release_port(rust_port_id id);
206-
rust_port *get_port_by_id(rust_port_id id);
207-
208198
// Use this function sparingly. Depending on the ref count is generally
209199
// not at all safe.
210200
intptr_t get_ref_count() const { return ref_count; }

0 commit comments

Comments
 (0)