what's a callback?

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
A few programmers I've known have used the term "callback", which
somehow related to executing a deferred subroutine or something. Does
anybody know anything about this term or its conventions? If I google
it, I just get a lot of telephone-type references.

I'm doing a thing now where, in an interrupt service routine, if I
think I'm out of time I set a flag to remind me to finish some chores
next time or so. I'm not sure if that qualifies as a "callback."

John




Re: what's a callback?
[F'up2 cut down --- should have been done by OP!]

Quoted text here. Click to load it

It's actually quite an established term in software API design, and it
means quite the same as its origin did in the realm of telephones: you
pass someone a number under which he should call you back, if
something particular happens.  In computers, that number is usually
the address of a function.

The canonical example of a callback function is a comparison function
passed to a generic sorting routine like qsort() in the C standard
library: you pass this information to qsort(), and whenever qsort()
needs to know which of two elements is larger, it'll call back your
application under this address to find out.

In more en-vogue terms of software design, the callback is the
procedural programmer's way of implementing what the OO guys call an
observer pattern.

Quoted text here. Click to load it

It doesn't.  That's just a flag.

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Re: what's a callback?
On 19 Dec 2004 20:20:59 GMT, the renowned Hans-Bernhard Broeker

Quoted text here. Click to load it

It's got to at least be a static flag, maybe more if the ISR is
designed to be re-entrant.


Best regards,
Spehro Pefhany
--
"it's the network..."                          "The Journey is the reward"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: what's a callback?
On Sun, 19 Dec 2004 15:54:38 -0500, Spehro Pefhany

Quoted text here. Click to load it


This is bare-metal assembly on a single-chip uP without MMU.
Everything's global, and everything's static. The isr runs
periodically at about 10 KHz, and just runs straight through to
completion every time, with some simple state flags to direct its
path. There's a block I *should* run every 5th time, but if something
more important needs to be done (namely collecting some energy
readings every time a laser fires) I set a flag to remind me to do the
deferred stuff some later pass.

Thanks to all for the "callback" explanation.

John


Re: what's a callback?
Quoted text here. Click to load it

I'd say that it's usually used when you have some kind of OS running.
You often specify the address of a routine to be executed when some kind
of I/O is complete.  The OS automatically invokes the "callback" routine
when whatever was being waited on is complete.

An interrupt handler might be deemed a callback routine, but I tend to
think of callbacks as being more of a one-off type of thing.

Quoted text here. Click to load it

I would call that a semaphore.  (actually, I would call it a switch or a
flag; someone with more professional education, or someone in personnel
might call it a semaphore ;-)


Re: what's a callback?
On Sun, 19 Dec 2004 12:09:55 -0800, the renowned John Larkin

Quoted text here. Click to load it

AFAIUI, a callback handler is essentially like asychronous logic- it's
a routine (that you register with some other piece of software) that
gets called when some specific set of conditions arise.

The business of sharing resources between threads in a threaded
environment (where you can, for some purposes, pretend that the
computer is executing multiple bits of software simultaneously) is
fairly complex and involves a lot of fairly odd terminology (mutex,
semaphore etc.)  Most of my stuff doesn't have an RTOS so neither the
facilities or the complexity are there.


Best regards,
Spehro Pefhany
--
"it's the network..."                          "The Journey is the reward"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: what's a callback?
On Sun, 19 Dec 2004 15:43:11 -0500, Spehro Pefhany


Quoted text here. Click to load it

Little ticktock state machines can be very powerful in the right
circumstances. And very reliable.

John



Re: what's a callback?

Quoted text here. Click to load it

Like when the code executes it. You're talking about an event with a
callback. Normally you wait on that in a thread callback function.

Example: My sound recorder/scope. The soundcard API funcs set an
"Event" when there's data ready. I passed the address of a callback
to whatever it was. The callback function (I wrote) sets an event
and my processing thread (also has a callback) deals with the data
when it returns from WaitForSingleOblect(my_event_handle) . I think
I used an unneccessary indirection there, but wtf?

It would have been better to get a handle to that event and just
wait on it in a thread callback.

pass the thread callback func pointer to the API func.

then in my thread callback:

WaitForSingleObject(HANDLE event_handle);

But you need the event handle. So I set my own event in a separate
callback. Oh! I can't pass the thread callback to the api because it
gets called when the thread is created and the thread terminates
when the callback function returns.
Quoted text here. Click to load it

Thanks for the refresher.
--
Best Regards,
Mike

Re: what's a callback?

Quoted text here. Click to load it

No,

As you know.... and I don't,

You deal with interrupts on the basis of priority which means an interrupt
with higher priority interrupts the original interrupt.

After dealing with the important interrupt then the system 'fallsback' to
deal with the original interrupt(s)..... in order of prioratisation.

A 'callback' is a check to see if the nonprioritised thing that went
'squeek' really needed stroking in the first place.

DNA



Re: what's a callback?
Quoted text here. Click to load it

It's when you pass the address of a function to the operating
system. Often used to change the standard behaviour of gadgets
supplied by the OS, and where only your program 'knows' how
it should be done. Such an example is the open-file-box that
windows presents, which can have many different 'faces' depending
on the application that calls it. Some common options can be
changed by simple parameters, but if you want some really odd
behaviour, you write the tricky part of the code yourself and use a
callback. Also called a hook.

Quoted text here. Click to load it

Like you say, it's a flag. An alternative to a single flag
could be a stack or queue of 'things that need to be done'.
Or un-done ;)

--
Thanks, Frank.
(remove 'q' and 'invalid' when replying by email)



Re: what's a callback?


Quoted text here. Click to load it

Sounds simple enough- think I used something similar long before there
ever was such a word.



Re: what's a callback?

Quoted text here. Click to load it

It is a simple concept. I think the word 'hook was more common with
hardware interrupts or hardware related bios calls. Later, with
graphical user interfaces, Apple Mac and GEM, these mechanisms were
named callbacks. I guess, under that name they must be around for
20-25 years...

--
Thanks, Frank.
(remove 'q' and 'invalid' when replying by email)






Re: what's a callback?
Its funny how many answers it takes.  ;D

Quoted text here. Click to load it



Re: what's a callback?


Quoted text here. Click to load it

No- that is called "setting a flag to remind you to finish some chores"
and is an example of *re-entrant code*:-) Why in the world would that be
called "callback" unless you collect useless jargon?


Re: what's a callback?

Quoted text here. Click to load it

The isr is not re-entrant. It just runs periodically to completion (in
about 12 usec) and takes various paths each run, depending on what
needs to be done. The flag makes sure I get all the necessary stuff
done, sooner or later.

Quoted text here. Click to load it

Well, I like to learn things. Sorry.

John



Re: what's a callback?


Quoted text here. Click to load it

Yes it is:
"Used to describe code which can have multiple simultaneous,
interleaved, or nested invocations which will not interfere with each
other. This is important for parallel processing, recursive functions or
subroutines, and interrupt handling."

Quoted text here. Click to load it

Right- so that would be interleaved calls.


Re: what's a callback?

Quoted text here. Click to load it

That's all nice, but my isr isn't reentrant and is simply executed
periodically and runs to completion every time. There's no os, no
multitasking, no nested interrupts, the variables are static, so it
can't be re-entered. No interleaving, either, just a couple of
conditional branches within the routine.

I've written reentrant code (and reentrant isr's and three
multitasking RTOSs) and this ain't.

This is in a 68332 with 8-bit eprom. At startup time, the "prototype"
routine is copied from eprom into the CPU internal ram, where it's
executed. That's a lot faster.

John


Re: what's a callback?


Quoted text here. Click to load it
chores"
Quoted text here. Click to load it
be

That's certainly the most unique definition of re-entrancy that I've
seen.

When I think of re-entrant code, I think of code that has no local
variable storage associated to it.  I also think of code that can call
itself recursively or be executed in several threads across multiple
processors concurrently with only one copy in memory.


Re: what's a callback?


Quoted text here. Click to load it

I am not going to quibble with your high level semantics- if the code
can be re-entered to perform its processing to completion then it is
re-entrant, period. I am not interested into any universal
all-encompassing definitions. It is a *simple* ISR.


Re: what's a callback?

Quoted text here. Click to load it


So any code that's ever run more than once is "re-entrant"?

John


Site Timeline