223: def generate(format = :default)
224: template = FORMATS[format]
225:
226: raise ArgumentError, "invalid UUID format #{format.inspect}" unless template
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238: clock = @mutex.synchronize do
239: clock = (Time.new.to_f * CLOCK_MULTIPLIER).to_i & 0xFFFFFFFFFFFFFFF0
240:
241: if clock > @last_clock then
242: @drift = 0
243: @last_clock = clock
244: elsif clock == @last_clock then
245: drift = @drift += 1
246:
247: if drift < 10000 then
248: @last_clock += 1
249: else
250: Thread.pass
251: nil
252: end
253: else
254: next_sequence
255: @last_clock = clock
256: end
257: end until clock
258:
259: template % [
260: clock & 0xFFFFFFFF,
261: (clock >> 32) & 0xFFFF,
262: ((clock >> 48) & 0xFFFF | VERSION_CLOCK),
263: @sequence & 0xFFFF,
264: @mac & 0xFFFFFFFFFFFF
265: ]
266: end