-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft of counting lock #74
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package flexpret.core | ||
import chisel3._ | ||
import chisel3.util._ | ||
|
||
// A simple implementation of a set of counting locks; one counting lock per thread | ||
|
||
class CountingLockIO(implicit val conf: FlexpretConfiguration) extends Bundle { | ||
// current thread id, used for all 3 operations | ||
val tid = Input(UInt(conf.threadBits.W)) | ||
|
||
// input for increment operation | ||
val increment = Input(UInt(32.W)) | ||
|
||
// input for reset operation | ||
val reset = Input(Bool()) | ||
|
||
// inputs for lock_until operation | ||
val lock_wait = Input(Bool()) | ||
val lock_id = Input(UInt(conf.threadBits.W)) | ||
val lock_until = Input(UInt(32.W)) | ||
|
||
// outputs: sleep if the current thread should sleep, | ||
// wake for whether any thread should wake | ||
val sleep = Output(Bool()) | ||
val wake = Output(Vec(conf.threads, Bool())) | ||
|
||
|
||
def driveInputDefaults() = { | ||
tid := 0.U | ||
increment := 0.U | ||
reset := false.B | ||
lock_wait := false.B | ||
lock_id := 0.U | ||
lock_until := 0.U | ||
} | ||
} | ||
|
||
class CountingLock(implicit val conf: FlexpretConfiguration) extends Module { | ||
val io = IO(new CountingLockIO()) | ||
|
||
// value of counting lock owned by each thread | ||
val regValue = RegInit(VecInit(Seq.fill(conf.threads) { 0.U(32.W) })) | ||
|
||
// what value each thread is waiting for | ||
val regUntil = RegInit(VecInit(Seq.fill(conf.threads) { 0.U(32.W) })) | ||
|
||
// which counting lock each thread is waiting on. own id means not waiting on anything | ||
val regWaitingOn = RegInit(VecInit( Seq.tabulate(conf.threads)(n => n.U(conf.threadBits.W)) )) | ||
|
||
when(io.reset) { | ||
regValue(io.tid) := 0.U | ||
} .otherwise { | ||
regValue(io.tid) := regValue(io.tid) + io.increment | ||
} | ||
|
||
when (io.lock_wait) { | ||
regUntil(io.tid) := io.lock_until | ||
regWaitingOn(io.tid) := io.lock_id | ||
} | ||
|
||
io.sleep := io.lock_wait && io.lock_until > regValue(io.tid) | ||
for (tid <- 0 until conf.threads) { | ||
when ((tid.U =/= io.tid) && regWaitingOn(tid) === io.tid && regUntil(tid) <= regValue(io.tid)) { | ||
io.wake(tid) := true.B | ||
regWaitingOn(tid) := tid.U // set to not waiting on anything | ||
} .otherwise { | ||
io.wake(tid) := false.B | ||
} | ||
} | ||
Comment on lines
+62
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why must There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The when statement roughly translates to "if some other thread is waiting on the current thread, and the value that thread is waiting for has been met, wake that thread". Yes, you should only wake up when the thread you are waiting on does an increment operation. Maybe I should clarify this logic in comments? |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldnt this be moved inside the when statement above? We only put threads to sleep when they write to the wait register
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, thx